Managed type???
-
Hi, I'm writing a wrapper class. So i have a DLL that contains too many C functions. Anyway, one function has void pointer for out parameter (2nd parameter) like following; void Test(void* pIn, void* pOut); Function is trying to fill pOut parameter with some info. The problem has started here. I want to convert void* value String or StringBuilder class. I can do it under unmanaged C/C++ such as following code; char* cpTest = new char[100]; Test(.... , static_cast(cpTest)); Now, i want to do it under managed C++. Eventually, which way should i follow? Thank for your response... Orkun GEDiK
-
Hi, I'm writing a wrapper class. So i have a DLL that contains too many C functions. Anyway, one function has void pointer for out parameter (2nd parameter) like following; void Test(void* pIn, void* pOut); Function is trying to fill pOut parameter with some info. The problem has started here. I want to convert void* value String or StringBuilder class. I can do it under unmanaged C/C++ such as following code; char* cpTest = new char[100]; Test(.... , static_cast(cpTest)); Now, i want to do it under managed C++. Eventually, which way should i follow? Thank for your response... Orkun GEDiK
You can pass the character array. Use the native structure System::IntPtr for the Test() function parameter. You can then write Test(..., System::IntPtr((void*)cpTest); You can use it for both the in and out. Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.
-
You can pass the character array. Use the native structure System::IntPtr for the Test() function parameter. You can then write Test(..., System::IntPtr((void*)cpTest); You can use it for both the in and out. Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.
Thank you for your response. System has generated following error; VoidPtr.cpp(19) : error C2440: 'type cast' : cannot convert from '__wchar_t __gc[]' to 'void *' Can only convert a __gc array to or from Object * or Array * VoidPtr.cpp(19) : error C2440: 'type cast' : cannot convert from '__wchar_t __gc[]' to 'void *' Can only convert a __gc array to or from Object * or Array * The source code at below; #include "stdafx.h" #using #include using namespace System; using namespace System::Runtime::InteropServices; #pragma unmanaged // Assume this block inside of DLL void Test(void* pIn, void* pOut) { char* pTest = static_cast(pIn); pOut = static_cast("Internal\0"); } #pragma managed void WrapperTest(String* strIn, String* strOut) { Test(System::IntPtr((void*)strIn->ToCharArray()), System::IntPtr((void*)strOut->ToCharArray())); } int _tmain(void) { String* strTestA = new String("Hello"); String* strTestB = new String(""); WrapperTest(strTestA, strTestB); return 0; } Thank you... Orkun GEDiK
-
Thank you for your response. System has generated following error; VoidPtr.cpp(19) : error C2440: 'type cast' : cannot convert from '__wchar_t __gc[]' to 'void *' Can only convert a __gc array to or from Object * or Array * VoidPtr.cpp(19) : error C2440: 'type cast' : cannot convert from '__wchar_t __gc[]' to 'void *' Can only convert a __gc array to or from Object * or Array * The source code at below; #include "stdafx.h" #using #include using namespace System; using namespace System::Runtime::InteropServices; #pragma unmanaged // Assume this block inside of DLL void Test(void* pIn, void* pOut) { char* pTest = static_cast(pIn); pOut = static_cast("Internal\0"); } #pragma managed void WrapperTest(String* strIn, String* strOut) { Test(System::IntPtr((void*)strIn->ToCharArray()), System::IntPtr((void*)strOut->ToCharArray())); } int _tmain(void) { String* strTestA = new String("Hello"); String* strTestB = new String(""); WrapperTest(strTestA, strTestB); return 0; } Thank you... Orkun GEDiK
Now, I see the picture clearer. You do not need the WrapperTest function and the System::IntPtr I mentioned earlier. Get the char* and pass it to the Test method:
const wchar_t __pin* psz = PtrToStringChars( pString )
The following is defined in vcclr.h header file included in VC.NET. This the same function used by the CString to convert the System::String to CString.
PtrToStringChars(String* pString)
Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.
-
Now, I see the picture clearer. You do not need the WrapperTest function and the System::IntPtr I mentioned earlier. Get the char* and pass it to the Test method:
const wchar_t __pin* psz = PtrToStringChars( pString )
The following is defined in vcclr.h header file included in VC.NET. This the same function used by the CString to convert the System::String to CString.
PtrToStringChars(String* pString)
Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.
Very good and useful information for me. Thank you. I've changed my code to following form;
#include "stdafx.h" #using #include #include using namespace System; using namespace System::Runtime::InteropServices; #pragma unmanaged // Assume this block inside of DLL void Test(void* pIn, void* pOut) { char* pTest = static_cast(pIn); pOut = ("Internal\0"); } #pragma managed int _tmain(void) { String* strTestA = new String("Hello"); String* strTestB = new String(""); wchar_t __pin* pTestA = PtrToStringChars(strTestA); wchar_t __pin* pTestB = PtrToStringChars(strTestB); Test(static_cast(pTestA), static_cast(pTestB)); return 0; }
Now, i can pass the pointer into DLL function, but i can't get any data from DLL (2nd parameter of Test function. So, pOut should be filled some data at inside of DLL). Thank you for your help... Best Regards... Orkun GEDiK -
Very good and useful information for me. Thank you. I've changed my code to following form;
#include "stdafx.h" #using #include #include using namespace System; using namespace System::Runtime::InteropServices; #pragma unmanaged // Assume this block inside of DLL void Test(void* pIn, void* pOut) { char* pTest = static_cast(pIn); pOut = ("Internal\0"); } #pragma managed int _tmain(void) { String* strTestA = new String("Hello"); String* strTestB = new String(""); wchar_t __pin* pTestA = PtrToStringChars(strTestA); wchar_t __pin* pTestB = PtrToStringChars(strTestB); Test(static_cast(pTestA), static_cast(pTestB)); return 0; }
Now, i can pass the pointer into DLL function, but i can't get any data from DLL (2nd parameter of Test function. So, pOut should be filled some data at inside of DLL). Thank you for your help... Best Regards... Orkun GEDiKBut pTestB is not a buffer to receive. Simply define a buffer and pass it in the out parameter. There is no need for the strTestB.
char szTestB[BufferSize] = {0};
should be sufficient. In the Test() function, you can do something simply like this:
lstrcpy(pOut, "Internal");
Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.
-
But pTestB is not a buffer to receive. Simply define a buffer and pass it in the out parameter. There is no need for the strTestB.
char szTestB[BufferSize] = {0};
should be sufficient. In the Test() function, you can do something simply like this:
lstrcpy(pOut, "Internal");
Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.
Ok. That means, i can't use managed type for pOut parameter. Ok, now i'm happy. Thank you for your response. Good answer. Thank you. Best Regards, Orkun. Orkun GEDiK