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. C / C++ / MFC
  4. How to correctly pass a LPCSTR type parameter to a thread in DLL?

How to correctly pass a LPCSTR type parameter to a thread in DLL?

Scheduled Pinned Locked Moved C / C++ / MFC
14 Posts 5 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.
  • P Offline
    P Offline
    pcname
    wrote on last edited by
    #1

    I wrote a DLL with VC6.0. I want to pass a LPCSTR type parameter (file path) to a thread. Here is the code: DWORD WINAPI ThreadFunc(LPVOID lpParam) { char* fileLoct = (char*)lpParam; MessageBox(NULL, fileLoct, "Message", MB_OK | MB_ICONINFORMATION); return 0; } void __stdcall StartThread(LPCSTR flt) { HANDLE hThread; hThread = CreateThread(NULL, 0, ThreadFunc, (void *)flt, 0, NULL); CloseHandle(hThread); } I tested it with MessageBox and found the path was not correct at all. How to correctly pass a LPCSTR type parameter to a thread in DLL? Thank you.

    R L S F 5 Replies Last reply
    0
    • P pcname

      I wrote a DLL with VC6.0. I want to pass a LPCSTR type parameter (file path) to a thread. Here is the code: DWORD WINAPI ThreadFunc(LPVOID lpParam) { char* fileLoct = (char*)lpParam; MessageBox(NULL, fileLoct, "Message", MB_OK | MB_ICONINFORMATION); return 0; } void __stdcall StartThread(LPCSTR flt) { HANDLE hThread; hThread = CreateThread(NULL, 0, ThreadFunc, (void *)flt, 0, NULL); CloseHandle(hThread); } I tested it with MessageBox and found the path was not correct at all. How to correctly pass a LPCSTR type parameter to a thread in DLL? Thank you.

      R Offline
      R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      What was "not correct" about it?

      The difficult we do right away... ...the impossible takes slightly longer.

      P 1 Reply Last reply
      0
      • R Richard Andrew x64

        What was "not correct" about it?

        The difficult we do right away... ...the impossible takes slightly longer.

        P Offline
        P Offline
        pcname
        wrote on last edited by
        #3

        MessageBox display is not file path, instead it showed something "s" or "c", "7". this is what I don't understand.

        R 1 Reply Last reply
        0
        • P pcname

          MessageBox display is not file path, instead it showed something "s" or "c", "7". this is what I don't understand.

          R Offline
          R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          Are you calling the thread from a Unicode function? If you're passing a Unicode string to a function that expects an ANSI string, this is what you'll see. See: Converting Unicode and ANSI Strings[^] and: WideCharToMultiByte function[^]

          The difficult we do right away... ...the impossible takes slightly longer.

          P 1 Reply Last reply
          0
          • R Richard Andrew x64

            Are you calling the thread from a Unicode function? If you're passing a Unicode string to a function that expects an ANSI string, this is what you'll see. See: Converting Unicode and ANSI Strings[^] and: WideCharToMultiByte function[^]

            The difficult we do right away... ...the impossible takes slightly longer.

            P Offline
            P Offline
            pcname
            wrote on last edited by
            #5

            No, my function is not a Unicode function. my file path is C:\Downloads\HornSound.wav. I found if I added the following " MessageBox(NULL, loct, "Message1", NULL); " before "CloseHandle(hThread);". Both MessageBoxes (both in the function and in the thread) can correctly display the file path. But if I removed "MessageBox" in the function, then the MessageBox in the thread cannot correctly display the file path. What is the problem?

            1 Reply Last reply
            0
            • P pcname

              I wrote a DLL with VC6.0. I want to pass a LPCSTR type parameter (file path) to a thread. Here is the code: DWORD WINAPI ThreadFunc(LPVOID lpParam) { char* fileLoct = (char*)lpParam; MessageBox(NULL, fileLoct, "Message", MB_OK | MB_ICONINFORMATION); return 0; } void __stdcall StartThread(LPCSTR flt) { HANDLE hThread; hThread = CreateThread(NULL, 0, ThreadFunc, (void *)flt, 0, NULL); CloseHandle(hThread); } I tested it with MessageBox and found the path was not correct at all. How to correctly pass a LPCSTR type parameter to a thread in DLL? Thank you.

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

              What is thae value of flt at the point that you call the thread, where does it come from?

              P 1 Reply Last reply
              0
              • P pcname

                I wrote a DLL with VC6.0. I want to pass a LPCSTR type parameter (file path) to a thread. Here is the code: DWORD WINAPI ThreadFunc(LPVOID lpParam) { char* fileLoct = (char*)lpParam; MessageBox(NULL, fileLoct, "Message", MB_OK | MB_ICONINFORMATION); return 0; } void __stdcall StartThread(LPCSTR flt) { HANDLE hThread; hThread = CreateThread(NULL, 0, ThreadFunc, (void *)flt, 0, NULL); CloseHandle(hThread); } I tested it with MessageBox and found the path was not correct at all. How to correctly pass a LPCSTR type parameter to a thread in DLL? Thank you.

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

                What is thae value of flt at the point that you call the thread, where does it come from?

                1 Reply Last reply
                0
                • P pcname

                  I wrote a DLL with VC6.0. I want to pass a LPCSTR type parameter (file path) to a thread. Here is the code: DWORD WINAPI ThreadFunc(LPVOID lpParam) { char* fileLoct = (char*)lpParam; MessageBox(NULL, fileLoct, "Message", MB_OK | MB_ICONINFORMATION); return 0; } void __stdcall StartThread(LPCSTR flt) { HANDLE hThread; hThread = CreateThread(NULL, 0, ThreadFunc, (void *)flt, 0, NULL); CloseHandle(hThread); } I tested it with MessageBox and found the path was not correct at all. How to correctly pass a LPCSTR type parameter to a thread in DLL? Thank you.

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  What doesn't work? compiler error? What's the error? Runtime error? Describe it. Give people something to go on!

                  Steve

                  P 1 Reply Last reply
                  0
                  • L Lost User

                    What is thae value of flt at the point that you call the thread, where does it come from?

                    P Offline
                    P Offline
                    pcname
                    wrote on last edited by
                    #9

                    "flt" is the file path from VBA. it's "C:\Downloads\HornSound.wav." Sorry, it's my mistake. the "loct" in MessageBox should be replaced with "flt". I spent some time try to figure it out. So as I said, I added " MessageBox(NULL, flt, "Message1", NULL); " before "CloseHandle(hThread);". both messagebox can correctly display my file path "C:\Downloads\HornSound.wav." I also found if I replaced "MessageBox" with "Sleep(3000)" before "CloseHandle(hThread);" MessageBox in thread can correctly display my file path "C:\Downloads\HornSound.wav." if I add nothing and just removed "CloseHandle(hThread);", MessageBox in thread can also correctly display my file path "C:\Downloads\HornSound.wav." It seems I cannot close handle right after CreateThread. but that can cause memory leak?

                    L 1 Reply Last reply
                    0
                    • S Stephen Hewitt

                      What doesn't work? compiler error? What's the error? Runtime error? Describe it. Give people something to go on!

                      Steve

                      P Offline
                      P Offline
                      pcname
                      wrote on last edited by
                      #10

                      no compiler error. when I calling the DLL from VBA, the function just cannot pass a file path to the thread. File path in thread displayed as "s", "c", etc.

                      1 Reply Last reply
                      0
                      • P pcname

                        "flt" is the file path from VBA. it's "C:\Downloads\HornSound.wav." Sorry, it's my mistake. the "loct" in MessageBox should be replaced with "flt". I spent some time try to figure it out. So as I said, I added " MessageBox(NULL, flt, "Message1", NULL); " before "CloseHandle(hThread);". both messagebox can correctly display my file path "C:\Downloads\HornSound.wav." I also found if I replaced "MessageBox" with "Sleep(3000)" before "CloseHandle(hThread);" MessageBox in thread can correctly display my file path "C:\Downloads\HornSound.wav." if I add nothing and just removed "CloseHandle(hThread);", MessageBox in thread can also correctly display my file path "C:\Downloads\HornSound.wav." It seems I cannot close handle right after CreateThread. but that can cause memory leak?

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

                        The fact that it works when you add a delay suggests that the file path buffer is getting destroyed before the thread process has constructed the message box. You should ensure the buffer is preserved until the thread has finished using it, by some form of synchronisation. Or better still don't use threads unless they are serving some necessary function.

                        1 Reply Last reply
                        0
                        • P pcname

                          I wrote a DLL with VC6.0. I want to pass a LPCSTR type parameter (file path) to a thread. Here is the code: DWORD WINAPI ThreadFunc(LPVOID lpParam) { char* fileLoct = (char*)lpParam; MessageBox(NULL, fileLoct, "Message", MB_OK | MB_ICONINFORMATION); return 0; } void __stdcall StartThread(LPCSTR flt) { HANDLE hThread; hThread = CreateThread(NULL, 0, ThreadFunc, (void *)flt, 0, NULL); CloseHandle(hThread); } I tested it with MessageBox and found the path was not correct at all. How to correctly pass a LPCSTR type parameter to a thread in DLL? Thank you.

                          F Offline
                          F Offline
                          Frankie C
                          wrote on last edited by
                          #12

                          You are using an automatic variable sent from VB call. That variable, and the string contente, will be destroyed after the function that create the thread returns. The thread instead will be running and looking for the string at the address that you passed in the thread creation, but that address holds only garbage by then... To make it work create a local string in your code and define it 'static', copy the passed string there, then create the thread.

                          void __stdcall StartThread(LPCSTR flt)
                          {
                          HANDLE hThread;
                          static char *szStaticString[MAX_PATH];
                          strncpy(szStaticString, flt, MAX_PATH-1);
                          hThread = CreateThread(NULL, 0, ThreadFunc,
                          (void *)szStaticString, 0, NULL);

                          CloseHandle(hThread);
                          }

                          P 1 Reply Last reply
                          0
                          • F Frankie C

                            You are using an automatic variable sent from VB call. That variable, and the string contente, will be destroyed after the function that create the thread returns. The thread instead will be running and looking for the string at the address that you passed in the thread creation, but that address holds only garbage by then... To make it work create a local string in your code and define it 'static', copy the passed string there, then create the thread.

                            void __stdcall StartThread(LPCSTR flt)
                            {
                            HANDLE hThread;
                            static char *szStaticString[MAX_PATH];
                            strncpy(szStaticString, flt, MAX_PATH-1);
                            hThread = CreateThread(NULL, 0, ThreadFunc,
                            (void *)szStaticString, 0, NULL);

                            CloseHandle(hThread);
                            }

                            P Offline
                            P Offline
                            pcname
                            wrote on last edited by
                            #13

                            I didn't realize that string variable was destroyed after the function creating the thread returns. Now my DLL works great. Frankie, thank you so much.

                            F 1 Reply Last reply
                            0
                            • P pcname

                              I didn't realize that string variable was destroyed after the function creating the thread returns. Now my DLL works great. Frankie, thank you so much.

                              F Offline
                              F Offline
                              Frankie C
                              wrote on last edited by
                              #14

                              You're welcome. :) If you have to start more threads with different strings, I suggest to allocate dynamic memory for each string to make it local for that thread. The thread can release the memory when it doesn't need it anymore.

                              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