I use this tutorial as a base for my code in a 32-bit unmanaged DLL https://code.msdn.microsoft.com/CppHostCLR-e6581ee0
Say I want to call TestIntPtr
public class IntPtrTester { public static void TestIntPtr(IntPtr p) { MessageBox.Show("TestIntPtr Method was Called"); } public static void TestInt(int p) { MessageBox.Show("TestInt Method was Called"); } }
How to pass an IntPtr parameter if on the C ++ side it represents a descriptor? TestInt works, but for TestIntPtr I get an error that the method was not found. This is because the type of the parameter is incorrect.
In the code from the tutorial for TestInt, I use
// HDC dc; // The static method in the .NET class to invoke. bstr_t bstrStaticMethodName(L"TestInt"); SAFEARRAY *psaStaticMethodArgs = NULL; variant_t vtIntArg((INT) dc); variant_t vtLengthRet; ... psaStaticMethodArgs = SafeArrayCreateVector(VT_VARIANT, 0, 1); LONG index = 0; hr = SafeArrayPutElement(psaStaticMethodArgs, &index, &vtIntArg); if (FAILED(hr)) { wprintf(L"SafeArrayPutElement failed w/hr 0x%08lx\n", hr); goto Cleanup; }
The question is what is the correct code for TestIntPtr
// The static method in the .NET class to invoke. // HDC dc; bstr_t bstrStaticMethodName(L"TestIntPtr"); SAFEARRAY *psaStaticMethodArgs = NULL; variant_t vtIntArg((INT) dc); // what do I have to write here? variant_t vtLengthRet;
I tried:
variant_t vtIntArg((INT) dc); variant_t vtIntArg((UINT) dc); variant_t vtIntArg((long) dc); variant_t vtIntArg((UINT32) dc); variant_t vtIntArg((INT32) dc);
Maybe the CLR expects IUNKNOWN from IntPtr? But how to build such an instance? I tried calling the IntPtr constructor with this API, but it returns a variant of type V_INTEGER, so this is a closed loop.
I know that I can open the C # library using COM, and how to use the DllExports hack, I can also change the C # part to accept only int or uint. But all these methods are not related to the issue.
It currently works for me with the following C # helper
public class Helper { public static void help(int hdc) { IntPtrTester.TestIntPtr(new IntPtr(hdc)); } }
and
variant_t vtIntArg((INT32) dc);
in C ++. But this is ugly because I need this helper for a library that I cannot influence.