从C ++ / CLI指针转换为本机C ++指针 [英] Convert from C++/CLI pointer to native C++ pointer
问题描述
我遇到了将C ++ / CLI指针转换为本地C ++指针的问题。背景:
我使用C ++ / CLI编写一个Windows窗体应用程序。该应用程序调用多个COM接口。当通过COM接口创建对象的实例(在C ++ / CLR类中)时,我将(void **)(& provider)
参数 CoCreateInstance
,如:
I have run in to this problem of converting a C++/CLI pointer to a native C++ pointer. Heres the background:
I'm writing a windows forms application using C++/CLI. The application makes call into a number of COM Interfaces. When creating an instance (inside of a C++/CLR class) of a object through the COM interface, i pass in (void**)(&provider)
as the last argument to CoCreateInstance
, as in:
HRESULT CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, (void**)(&provider));
但是,我得到编译器错误:无法从 cli :: interior_ptr< ; Type>
到 void **
。
我做了一些研究,看起来它是一个问题的不同类型的指针在C + +和C + + / CLI。任何人都有这方面的任何知识,或许一个小技巧如何可以解决? Thanx提前!
However, i get the compiler error: cannot convert from cli::interior_ptr<Type>
to void **
.
I've done som research into this, and it looks like it is a problem of different types of pointers in C++ and C++/CLI. Anyone has any knowledge about this, and maybe a tip on how it could be fixed? Thanx in advance!
首先,,x帮助你!
pin_ptr
,但这使编译器抱怨从 interior_ptr
转换为 pin_ptr
。如果我尝试使用 interior_ptr
:
As Freich suggested, i tried to use the pin_ptr
, but this instead made the compiler complaining about problems converting from interior_ptr
to pin_ptr
. If i instead try to use the interior_ptr
as in:
pin_ptr<void *> pinnedPtr = &provider;
CoCreateInstance(CLSID_MSPRProvider, NULL, CLSCTX_LOCAL_SERVER, IID_IMSPRProvider, ( void** )pinnedPtr);
我无法从 interior_ptr
code> interior_ptr 。
这一切归结为将 interior_ptr
转换为 void **
的问题。像这样:(void **)interiorPtr
,其中 interiorPtr
当然是 interior_ptr
。任何想法?
I get cannot convert from interior_ptr
to interior_ptr
.
It all boils down to the problem of converting the interior_ptr
to void**
. Like this: (void**)interiorPtr
, where interiorPtr
of course is an interior_ptr
. Any ideas in this?
推荐答案
我相信你必须将指针标记为固定将字节复制到一些非托管内存区域,然后使用指针。例如,这里有一段代码,我曾经在某处将一个指向受管System :: String的指针转换为UTF-8编码的非托管std :: string:
I believe you have to mark the pointer as 'pinned' (in the managed code) and then copy the bytes over to some unmanaged memory region, and then use the pointer to that. For instance, here's a piece of code I once got somewhere which converts a pointer to a managed System::String to an UTF-8 encoded unmanaged std::string:
std::string managedStringToStlString( System::String ^s )
{
Encoding ^u8 = Encoding::UTF8;
array<unsigned char> ^bytes = u8->GetBytes( s );
pin_ptr<unsigned char> pinnedPtr = &bytes[0];
return string( (char*)pinnedPtr );
}
注意我如何得到一个'固定' code> GetBytes()函数返回的code> bytes 数组,然后将其转换为 char * code>,然后将其传递给
std :: string
构造函数。我相信这是必要的,以避免管理的内存子系统在内存中移动 bytes
数组,同时我将字节复制到STL字符串。
Note how I've got to get a 'pinned' pointer to the bytes
array returned by the GetBytes()
function, and then cast that to char*
before passing it to the std::string
constructor. I believe this is necessary to avoid that the managed memory subsystem moves the bytes
array around in memory while I'm copying the bytes into the STL string.
在您的情况下,尝试替换
In your case, try replacing
CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, (void**)(&provider));
pin_ptr<void *> pinnedPtr = &provider;
CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, (void**)pinnedPtr);
并查看其是否有效。
这篇关于从C ++ / CLI指针转换为本机C ++指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!