我的代码有什么问题,如何正确执行?
(我认为使用Cli指针是错误的,因为我收到一个不能从System :: IntPtr ^转换为System :: IntPtr的错误,但是我不知道如何使它能正常工作. ..)
我的C MFC代码:
HWND myHandle= this->GetSafeHwnd();m_CLIDialog->UpdateHandle(myHandle);
我的C#代码:
public voID UpdateHandle(IntPtr mHandle){ ......}
我的Cli代码:
voID CLIDialog::UpdateHandle(HWND hWnd){ System::IntPtr^ managedhWnd = gcnew System::IntPtr(); HWND phWnd; // object on the native heap try { phWnd = (HWND)managedhWnd->topointer(); *phWnd = *hWnd; //Deep-copy the Native input object to Managed wrapper. m_pManagedData->CSharpControl->UpdateHandle(managedhWnd); }
当前在m_pManagedData-> CSharpControl-> UpdateHandle(managedhWnd)中出现错误(无法从IntPtr ^转换为IntPtr);
如果我将Cli代码更改为:
voID CLIDialog::UpdateHandle(HWND hWnd){ System::IntPtr managedhWnd; HWND phWnd; // object on the native heap try { phWnd = (HWND)managedhWnd.topointer(); *phWnd = *hWnd; //Deep-copy the Native input object to Managed wrapper. m_pManagedData->CSharpControl->UpdateHandle(managedhWnd); }
所以在这种情况下,C#中得到的值为0.
如何使其正常工作?
解决方法 要将HWND(这只是一个指针)转换为IntPtr,您只需要调用它的构造函数,并且不需要gcnew,因为它是一个值类型.所以这应该可以将HWND从本机传递到管理:
voID CLIDialog::UpdateHandle( HWND hWnd ){ IntPtr managedHWND( hwnd ); m_pManagedData->CSharpControl->UpdateHandle( managedHWND );}
这是一个可以从托管代码调用的函数,并从本地代码获取本地HWND:
voID SomeManagedFunction( IntPtr hWnd ){ HWND nativeHWND = (HWND) hWnd.topointer(); //...}总结
以上是内存溢出为你收集整理的c# – 将HWND转换为IntPtr(CLI)全部内容,希望文章能够帮你解决c# – 将HWND转换为IntPtr(CLI)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)