Thread: LPVOID to char *

  1. #16
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by stovellp
    I cannot change the definition of GetServerVariable(), and so I must make it work without changing the way the function looks to the client. Thats why I must find a way to make it work with an LPVOID rather than any other type of function.
    One last try from my side. I think my memcpy suggestion was valid, so I'll give you an example. If it's not what you want... err, ya, 14 posts for nothing

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    const int g_iBufferSize= 100;
    
    void vtemp(LPVOID pcBuffer, int *iSize) {
    	char szTemp[g_iBufferSize];
    	
    	// fill szTemp with some junk
    	char *pcSomeJunk= "somejunk";
    	strncpy(szTemp, pcSomeJunk, *iSize);
    	// end of junk
    
    	// limit iSize as defined by ms spec
    	// min() is better, just don't know if it's standard
    	// strlen should better be cached
    	if (*iSize > strlen(pcSomeJunk))
    		*iSize= strlen(pcSomeJunk);
    	
    	memcpy(pcBuffer, &szTemp, *iSize);
    }
    
    int main(int argc, char *argv[])
    {
    	char	szBuffer[100];
    	int	iSize= sizeof(szBuffer);
    
    	vtemp(szBuffer, &iSize);
    
    	cout << szBuffer;
      
    	system("PAUSE");	
    	return 0;
    }
    I'm not a fan of cstrings - they are slow like a snail and pose a great threat to application security. I hardly ever use them, which is the reason why the above code might be a prime example of insecure code. Better check it twice.


    edit: I know, to use both strcpy and memcpy is somewhat redundant. This is only because I needed to supply some kind of data to fill the buffer and I don't know in what format your data is stored. In my above example you could even use strncpy to directly copy the string to a typecasted LPVOID pcBuffer (cast as (char *)). Ya.... err. Hope that somehow helps.
    Last edited by darksaidin; 10-01-2003 at 07:23 AM.
    [code]

    your code here....

    [/code]

  2. #17
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Thanks heaps Darksaidin! That seems to almost be working. Heres my code for GetServerVariable export:

    PHP Code:
    BOOL WINAPI pGetServerVariable(HCONN hConnLPSTR lpszVariableNameLPVOID *lpvBufferLPDWORD lpdwSize)
    {
        
    char szTemp[1024];

        
    char pcSomeJunk "POST\0";
        
    strncpy(szTemppcSomeJunk, *lpdwSize);

        
    cout << "GetServerVariable() called: " << lpszVariableName << endl;
        
        if (!
    strcmpi(lpszVariableName"HTTP_COOKIE"))
        {
            
    strncpy(szTemp"user=john", *lpdwSize);
        }
        else if (!
    strcmpi(lpszVariableName"REQUEST_METHOD"))
        {
            
    strncpy(szTemp"POST", *lpdwSize);
        }
        else if (!
    strcmpi(lpszVariableName"ALL_HTTP"))
        {
            
    strncpy (szTemp "POST /TEST.php HTTP/1.0\n\
    From: [email][email protected][/email]\n\
    User-Agent: HTTPTool/1.0\n\
    Content-Type: application/x-www-form-urlencoded\n\
    Content-Length: 9\n\
    \n\
    user=john\n\
    "
    , *lpdwSize);
        }
        else if (!
    strcmpi(lpszVariableName"HTTPS"))
        {
            
    strncpy(szTemp"OFF", *lpdwSize);
        }
        else if (!
    strcmpi(lpszVariableName"SCRIPT_NAME"))
        {
            
    strncpy(szTemp"/test.php\0", *lpdwSize);
        }
        else if (!
    strcmpi(lpszVariableName"SCRIPT_FILENAME"))
        {
            
    strncpy(szTemp"c:\\sws\\webroot\\test.php", *lpdwSize);
        }
        else if (!
    strcmpi(lpszVariableName"CONTENT_TYPE"))
        {
            
    strncpy(szTemp"application/x-www-form-urlencoded", *lpdwSize);
        }
        else if (!
    strcmpi(lpszVariableName"CONTENT_LENGTH"))
        {
            
    strncpy(szTemp"9", *lpdwSize);
        }
        else 
    strncpy(szTemp"", *lpdwSize);

        if (*
    lpdwSize strlen(pcSomeJunk))
        {
            *
    lpdwSize strlen(pcSomeJunk);
        }
        
        
    memcpy(lpvBuffer, &szTemp, *lpdwSize);
        
    cout << "Sent them: \"" << (char *)lpvBuffer << endl;
        return 
    true;

    You'll see it works a lot like the code you gave, but its acting rather strangly. For REQUEST_METHOD, PHP prints this:
    Code:
    POST</td><td class="v">14</td></tr> <tr><td class="e">register_argc_argv</td><td class="v">On</td><td class="v">On</td></tr> <tr><td class="e">register_globals</td><td class="v">Off</td><td class="v">Off</td></tr> <tr><td class="e">report_memleaks</td><td class="v">On</td><td class="v">On</td></tr> <tr><td class="e">safe_mode<˜ð
    I think what happened is they use the same buffer for storing the return vaule as they do for some output. When &szTemp is copied into lpvBuffer it seems to only copy the first 4 bytes. I changed the memcopy function to be:
    memcpy(..,.., *lpdwSize+1) and it seems to have worked for REQUEST_METHOD, but not for the other ones. I just don't know what to do!

  3. #18
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Ahhh don't worry, I think I've figured it out. I changed the end of the function to:

    PHP Code:
    else strncpy(szTemp"", *lpdwSize);

        *
    lpdwSize strlen(szTemp);
        
    szTemp[strlen(szTemp) +] = '\0';
        
        
    memcpy(lpvBuffer, &szTempstrlen(szTemp) + 1);
        
    cout << "Sent them: \"" << (char *)lpvBuffer << endl;
        return 
    true
    So that it makes sure theres a \0 at the end of the string, and its outputting everything perfectly. Thankyou guys so much!!!

  4. #19
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Great

    Just make sure there is no chance for buffer overflows.
    [code]

    your code here....

    [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM