与C#中的C ++“ this”指针等效

时间:2018-06-30 14:26:56

标签: c# c++ pointers this

我正在尝试将可以成功运行的C ++程序转换为C#,并且目前只能初始化音频输出设备。我想知道C#中“ this”指针的等效内容。

下面是有效的C ++代码:

DWORD WINAPI SCFStartOutSound(SCFHANDLE sohOwnerHandle, SCFHANDLE *pscfHandle, SCFEventOutSoundCallback lpfnEventCallback, BSTR FileName);

CCComBSTR FileName = "Out_DxSpeaker.dll";
HANDLE OutSoundHandle = NULL;
SCFStartOutSound(this, &OutSoundHandle, OnSCFEventOutSoundCallback, FileName);

班级:

CTestAppDlg::CTestAppDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestAppDlg::IDD, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

Typedef:

typedef HANDLE SCFHANDLE;

结果:音频输出设备已正确初始化

Initialized output device

现在这是我的C#翻译代码:

[DllImport(DllFileName.dll)]
unsafe public static extern uint SCFStartOutSound(IntPtr OwnerHandle,IntPtr* soundoutHandle, SCFEventOutSoundCallback EventCallback, [MarshalAs(UnmanagedType.BStr)] string fileName);

  unsafe
    {
        IntPtr soundOutPtr;
        uint res = SCFStartOutSound(this.Handle, &soundOutPtr, onSoundOutEventCallback,"Out_DxSpeaker.dll");
        MessageBox.Show(res.ToString());
    }

此代码导致System.AccessViolationException。

我知道C ++中的'this'是保存当前对象的内存地址的指针,而C#中的'this'引用该类的当前实例。

0 个答案:

没有答案