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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. System::String ^ to LPCWSTR conversion?

System::String ^ to LPCWSTR conversion?

Scheduled Pinned Locked Moved Managed C++/CLI
question
8 Posts 3 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.
  • M Offline
    M Offline
    Mattzimmerer
    wrote on last edited by
    #1

    I just recently posted a question very similar to this_(System::String ^ to LPCVOID conversion?)_, it was answered but I still must be missing something. In this case I need a LPCWSTR. What I've tried should be evident in the following:

    ......
    char* pProcName = (char*)(void*)Marshal::StringToHGlobalAnsi(this->ProcName->Text);//WORKS GREAT
    LPCWSTR pProcName2 = (LPCWSTR)(void*)Marshal::StringToHGlobalAnsi(this->ProcName->Text);//THIS LINE FILLS pProcName2 WITH GARBAGE....

    LPCWSTR test2 = (LPCWSTR) pProcName;//test2 is now GARBAGE ARRG

    HWND windowHandle = FindWindowW(NULL, (LPCWSTR) pProcName);//lets see if it will find GARBAGE...
    if (windowHandle)
    MessageBox::Show("SUCCESS");//Wishfull thinking.... BLAAH
    ........

    I guess one option is to convert a char* to wchar_t*? If somehow I could make the conversion correctly, uhg. I really just need someone who knows whats up to point it out to me lol...

    L M M 3 Replies Last reply
    0
    • M Mattzimmerer

      I just recently posted a question very similar to this_(System::String ^ to LPCVOID conversion?)_, it was answered but I still must be missing something. In this case I need a LPCWSTR. What I've tried should be evident in the following:

      ......
      char* pProcName = (char*)(void*)Marshal::StringToHGlobalAnsi(this->ProcName->Text);//WORKS GREAT
      LPCWSTR pProcName2 = (LPCWSTR)(void*)Marshal::StringToHGlobalAnsi(this->ProcName->Text);//THIS LINE FILLS pProcName2 WITH GARBAGE....

      LPCWSTR test2 = (LPCWSTR) pProcName;//test2 is now GARBAGE ARRG

      HWND windowHandle = FindWindowW(NULL, (LPCWSTR) pProcName);//lets see if it will find GARBAGE...
      if (windowHandle)
      MessageBox::Show("SUCCESS");//Wishfull thinking.... BLAAH
      ........

      I guess one option is to convert a char* to wchar_t*? If somehow I could make the conversion correctly, uhg. I really just need someone who knows whats up to point it out to me lol...

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      According to the MSDN Documentation[^] StringToHGlobalAnsi() returns a char*, i.e. a pointer to ASCII characters. In order to use this as wide characters you will need to do a string conversion, you cannot just cast it and expect it to work. Use mbtowc() or similar to convert from ASCII to wide characters.

      M 1 Reply Last reply
      0
      • L Lost User

        According to the MSDN Documentation[^] StringToHGlobalAnsi() returns a char*, i.e. a pointer to ASCII characters. In order to use this as wide characters you will need to do a string conversion, you cannot just cast it and expect it to work. Use mbtowc() or similar to convert from ASCII to wide characters.

        M Offline
        M Offline
        Mattzimmerer
        wrote on last edited by
        #3

        Yea, I didnt expect it to work. But when you have no other ideas... lol thanks man! Ill give it a go!

        L 1 Reply Last reply
        0
        • M Mattzimmerer

          I just recently posted a question very similar to this_(System::String ^ to LPCVOID conversion?)_, it was answered but I still must be missing something. In this case I need a LPCWSTR. What I've tried should be evident in the following:

          ......
          char* pProcName = (char*)(void*)Marshal::StringToHGlobalAnsi(this->ProcName->Text);//WORKS GREAT
          LPCWSTR pProcName2 = (LPCWSTR)(void*)Marshal::StringToHGlobalAnsi(this->ProcName->Text);//THIS LINE FILLS pProcName2 WITH GARBAGE....

          LPCWSTR test2 = (LPCWSTR) pProcName;//test2 is now GARBAGE ARRG

          HWND windowHandle = FindWindowW(NULL, (LPCWSTR) pProcName);//lets see if it will find GARBAGE...
          if (windowHandle)
          MessageBox::Show("SUCCESS");//Wishfull thinking.... BLAAH
          ........

          I guess one option is to convert a char* to wchar_t*? If somehow I could make the conversion correctly, uhg. I really just need someone who knows whats up to point it out to me lol...

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Mattzimmerer wrote:

          I guess one option is to convert a char* to wchar_t*?

          Why use StringToHGlobalAnsi if you don't want an ANSI string? Maybe try StringToHGlobalUni. And don't forget to free the allocated memory when you're done with it! Since you're wanting wide chars, you also can use PtrToStringChars (from vcclr.h).

          Mattzimmerer wrote:

          need someone who knows whats up to point it out to me

          The documentation knows what's up! A bunch of interop examples[^] including How to: Access Characters in a System::String[^] Calling Native Functions from Managed Code[^]

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          modified on Thursday, January 7, 2010 3:42 PM

          1 Reply Last reply
          0
          • M Mattzimmerer

            Yea, I didnt expect it to work. But when you have no other ideas... lol thanks man! Ill give it a go!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Mattzimmerer wrote:

            Yea, I didnt expect it to work.

            That suggests you would be well advised to spend some time learning what casts are all about. Remember that all the answers are somewhere in MSDN. I know it's not the easiest site to navigate, but the new Bing search is pretty good; it's where I found your answer!

            1 Reply Last reply
            0
            • M Mattzimmerer

              I just recently posted a question very similar to this_(System::String ^ to LPCVOID conversion?)_, it was answered but I still must be missing something. In this case I need a LPCWSTR. What I've tried should be evident in the following:

              ......
              char* pProcName = (char*)(void*)Marshal::StringToHGlobalAnsi(this->ProcName->Text);//WORKS GREAT
              LPCWSTR pProcName2 = (LPCWSTR)(void*)Marshal::StringToHGlobalAnsi(this->ProcName->Text);//THIS LINE FILLS pProcName2 WITH GARBAGE....

              LPCWSTR test2 = (LPCWSTR) pProcName;//test2 is now GARBAGE ARRG

              HWND windowHandle = FindWindowW(NULL, (LPCWSTR) pProcName);//lets see if it will find GARBAGE...
              if (windowHandle)
              MessageBox::Show("SUCCESS");//Wishfull thinking.... BLAAH
              ........

              I guess one option is to convert a char* to wchar_t*? If somehow I could make the conversion correctly, uhg. I really just need someone who knows whats up to point it out to me lol...

              M Offline
              M Offline
              Mattzimmerer
              wrote on last edited by
              #6

              Okay, you guys were a ton of help, but there is still something I am missing. Why dont I just show you what I am trying to do: I am trying to get a name from a windows forms textbox converted so that I can use it in FindWindowW() so that I can send a PID out to a driver this code is pretty messy atm, I usually clean it up after I get the proper way to do something. (imo lol)

              char* pProcName = (char*)(void*)Marshal::StringToHGlobalUni(this->ProcName->Text);

              wchar_t ProcNameWSTR, *pProcNameWSTR = &ProcNameWSTR;

              const wchar_t tester[11] = L"Calculator";

              int lengthmbc = mbtowc(pProcNameWSTR,pProcName,sizeof pProcName);

              unsigned long Returned;
              input bInput; //pre defined struct showing the members
              bInput.bytestoread;
              bInput.processid;
              bInput.startaddress;

              HWND windowHandle = FindWindowW(NULL, pProcNameWSTR);
              HWND windowHandle2 = FindWindowW(NULL, (LPCWSTR) &tester);
              if (windowHandle)
              MessageBox::Show("pProcNameWSTR success");//FAIL
              if (windowHandle2)
              MessageBox::Show("tester wchar_t success");//SUCCESS

              DWORD* processID = new DWORD;
              GetWindowThreadProcessId(windowHandle, processID);

              DeviceIoControl(
              hFile, // Device handle
              IOCTL_MZ_READMEMORY, // Code
              (LPVOID) &bInput, // Buffer TO driver
              sizeof bInput, // Size of InBuffer
              NULL, // Buffer FROM driver
              0, // Size of OutBuffer
              &Returned, // Bytes output
              (LPOVERLAPPED) NULL); // Overlapped struc

              the problem is that I cannot get the contents of ProcNameWSTR to match my tester[11] (the first if check fails and the second succeeds) any insight?

              M 1 Reply Last reply
              0
              • M Mattzimmerer

                Okay, you guys were a ton of help, but there is still something I am missing. Why dont I just show you what I am trying to do: I am trying to get a name from a windows forms textbox converted so that I can use it in FindWindowW() so that I can send a PID out to a driver this code is pretty messy atm, I usually clean it up after I get the proper way to do something. (imo lol)

                char* pProcName = (char*)(void*)Marshal::StringToHGlobalUni(this->ProcName->Text);

                wchar_t ProcNameWSTR, *pProcNameWSTR = &ProcNameWSTR;

                const wchar_t tester[11] = L"Calculator";

                int lengthmbc = mbtowc(pProcNameWSTR,pProcName,sizeof pProcName);

                unsigned long Returned;
                input bInput; //pre defined struct showing the members
                bInput.bytestoread;
                bInput.processid;
                bInput.startaddress;

                HWND windowHandle = FindWindowW(NULL, pProcNameWSTR);
                HWND windowHandle2 = FindWindowW(NULL, (LPCWSTR) &tester);
                if (windowHandle)
                MessageBox::Show("pProcNameWSTR success");//FAIL
                if (windowHandle2)
                MessageBox::Show("tester wchar_t success");//SUCCESS

                DWORD* processID = new DWORD;
                GetWindowThreadProcessId(windowHandle, processID);

                DeviceIoControl(
                hFile, // Device handle
                IOCTL_MZ_READMEMORY, // Code
                (LPVOID) &bInput, // Buffer TO driver
                sizeof bInput, // Size of InBuffer
                NULL, // Buffer FROM driver
                0, // Size of OutBuffer
                &Returned, // Bytes output
                (LPOVERLAPPED) NULL); // Overlapped struc

                the problem is that I cannot get the contents of ProcNameWSTR to match my tester[11] (the first if check fails and the second succeeds) any insight?

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                Mattzimmerer wrote:

                I cannot get the contents of ProcNameWSTR to match my tester[11]

                ProcNameWSTR is a single wchar_t, so isn't very useful here. Plus you don't even use it anywhere. Marshal::StringToHGlobalUni returns a const pointer to a wchar_t string, so casting it to a char* then converting it to a wchar_t string isnt' going to work. Try:

                const wchar_t *pProcNameWSTR = (const wchar_t *)(void *)Marshal::StringToHGlobalUni(this->ProcName->Text);

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                M 1 Reply Last reply
                0
                • M Mark Salsbery

                  Mattzimmerer wrote:

                  I cannot get the contents of ProcNameWSTR to match my tester[11]

                  ProcNameWSTR is a single wchar_t, so isn't very useful here. Plus you don't even use it anywhere. Marshal::StringToHGlobalUni returns a const pointer to a wchar_t string, so casting it to a char* then converting it to a wchar_t string isnt' going to work. Try:

                  const wchar_t *pProcNameWSTR = (const wchar_t *)(void *)Marshal::StringToHGlobalUni(this->ProcName->Text);

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  M Offline
                  M Offline
                  Mattzimmerer
                  wrote on last edited by
                  #8

                  worked great thank you. You know, I tried something like this, I just didnt make it a const, perhaps thats the reason it didnt work... Either that or I threw in some ANSII.. o well thanks man!

                  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