CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1

    Angry CreateThread LPVOID Struct Char * Issue

    Alright Im new and this is my first post ive always read CodeGuru but just now got stuck with an issue that pretty much conserns me so i found myself here and Im glad to join.

    Ill try to make that as strate forward as possable

    I got a DLL I made VIA STDCall a Function call Test it takes 3 Params (INT, Char*, Long)

    The first is a Port Number, the 2nd is a String that will hold "127.0.0.1", and the Length of the string passed of "127.0.0.1" which will change Via Application passing the Args

    Now im calling this function from VB6 using FromUnicode option to format "127.0.0.1" then formatting it further once it Hits the C++ Dll Function Thats fine...

    Now it takes that String and put it into a Structure that looks like this
    Code:
    typedef struct {
    unsigned int Port;
    char * IP;
    } ConnectionInformation;
    I then use
    Code:
    char IPModded[16]; //Max Len of Ip is 15 + Null
    	strcpy(IPModded, ParamIP);
    	IPModded[ParamLenOfIP] = '\0'; // Cut Of Crap from End Of String
    
    	ConnectionInformation MyStruct;
    	MyStruct.IP = IPModded;
    	MyStruct.Port = ParamPort;
    	MessageBox(0,LPCSTR(MyStruct.IP),LPCSTR("before it goes through"),0);
    	CreateThread(NULL, 0, InitSocketTest, reinterpret_cast<LPVOID>(&MyStruct), 0, 0);
    now another function resides in the same dll that CreateThread calls.. now the issue is that right when the function is called I use this
    Code:
    DWORD WINAPI InitSocketTest(LPVOID lp)
    
    {
    	//ConnectionInformation * MyInformation = (ConnectionInformation *)lp;
    	ConnectionInformation * MyInformation = reinterpret_cast<ConnectionInformation *>(lp);
    	unsigned int nPort=MyInformation->Port; //65535
    	MessageBox(0,LPCSTR(MyInformation->IP),LPCSTR("Information"),0);
    }
    and somewhere in between The CreateThread and Messagebox it Messes up beucase about 10&#37; of the time it shows 127.0.0.1 in the messagebox and the other it Crashes or displays Junk evenly... Any Ideas im sure its something reall simple that I dont see,

    Now once I get the Char Pointer issue handled I am just passing it like this to a Socket Stucture
    Is this the correct way to use it?
    Code:
     
    if ((RemoteSin.sin_addr.S_un.S_addr = inet_addr(MyInformation->IP)) == INADDR_NONE)
    Thankyou!
    Last edited by AgentSmithers; December 18th, 2008 at 10:37 PM.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: CreateThread LPVOID Struct Char * Issue

    That's normal.
    Code:
    MyStruct.IP = IPModded;
    IP is a pointer to char. The above line says: "let IP point to the address of buffer IPModded". Then you create a new thread and probably return from that function. But then, IPModded, which is a local variable gets out of scope, and IP will point to garbage memory. So, you're lucky that 10&#37; of the time you have it working.

    To solve it, you must allocate memory for IP, and copy the content of IPModded to IP. Actually, you don't need IPModded at all.

    Change
    Code:
    char IPModded[16]; //Max Len of Ip is 15 + Null
    	strcpy(IPModded, ParamIP);
    	IPModded[ParamLenOfIP] = '\0'; // Cut Of Crap from End Of String
    
    	ConnectionInformation MyStruct;
    	MyStruct.IP = IPModded;
    to
    Code:
    ConnectionInformation MyStruct;
    
    MyStruct.IP = new char[ParamLenOfIP + 1];
    strcpy(MyStruct.IP, ParamIP);
    MyStruct.IP[ParamLenOfIP] = '\0'; // Cut Of Crap from End Of String
    But you have to make sure, when MyStruct is no longer needed, that you release the memory allocated for IP.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3

    Re: CreateThread LPVOID Struct Char * Issue

    You Seem Absolotly Correct just by Reading Thankyou sir, it seems to still be returning Garbage for some reason and it probley is still due to a scope issue. but now its messageboxing junk still BUT the same junk its time.

    Edit: It being a scope issue I moved ConnectionInformation MyStruct; to the Glocal Declare part of the code and it seems to have fixed it! I hope its a Working 100&#37; Fix, Thank alot Cilu!
    Last edited by AgentSmithers; December 19th, 2008 at 04:52 AM.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: CreateThread LPVOID Struct Char * Issue

    Well, right, of course. Actually I missed about the scope of the MyStruct object, but that obvious, as you figured out.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured