Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Managed type???

Managed type???

Scheduled Pinned Locked Moved Managed C++/CLI
c++helpquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Orkun GEDiK
    wrote on last edited by
    #1

    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

    P 1 Reply Last reply
    0
    • O 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

      P Offline
      P Offline
      Paul Selormey
      wrote on last edited by
      #2

      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.

      O 1 Reply Last reply
      0
      • P Paul Selormey

        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.

        O Offline
        O Offline
        Orkun GEDiK
        wrote on last edited by
        #3

        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

        P 1 Reply Last reply
        0
        • O 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

          P Offline
          P Offline
          Paul Selormey
          wrote on last edited by
          #4

          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.

          O 1 Reply Last reply
          0
          • P Paul Selormey

            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.

            O Offline
            O Offline
            Orkun GEDiK
            wrote on last edited by
            #5

            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

            P 1 Reply Last reply
            0
            • O 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 GEDiK

              P Offline
              P Offline
              Paul Selormey
              wrote on last edited by
              #6

              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.

              O 1 Reply Last reply
              0
              • P Paul Selormey

                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.

                O Offline
                O Offline
                Orkun GEDiK
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups