Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I want to convert a string to LPVOID.
string strData = "Some Data";
LPVOID lpBuffer = (LPVOID)strData.c_str();

But it does not show any data in lpBuffer.

Please help me.

Thanks,
Avikal
Posted
Updated 16-Feb-10 21:42pm
v2

Where do you look for "any data in lpBuffer"? Just look at memory for address of your lpBuffer. It got your "Some Data" :)
 
Share this answer
 
As Jast_in said, set a breakpoint after the line assigning the value to the void pointer. The select Debug -> Windows -> Memory and select a memory window. Here type in the address of the void pointer.
 
Share this answer
 
I am sending the data of LpBuffer to a web page via using method WinHttpSendRequest. In this method I have to pass the the data in the form of LPVOID and I am reading data from a text file which I get in string format that why I need this conversion.
 
Share this answer
 
It is impossible to help you cause nobody knows what you are doing. Question was "How to convert string to LPVOID". You've done that as well. Nobody can explain why the function "does not show any data at that page", until you don't provide your code. But... the most possible reason is the following:

<br />
string strData = "Some Data";<br />
LPVOID lpBuffer = (LPVOID)strData.c_str();<br />


lpBuffer gets the value "Some data"

<br />
strData = "another data";<br />


lpBuffer gets the value "another data"

Debug will help you... pay attention to WHERE the value of "strData" is changed!
 
Share this answer
 
v3
You should pass to "SendRequest" not "&lpBuffer" but lpBuffer
 
Share this answer
 
As I've already said strData may change its value. Static declaration of lpBuffer will change nothing. If strData don't change its value before calling WinHttpSendRequest then it should be passed to WinHttpSendRequest as "Data to be send" in buffer.

But the problem is changed from "How to convert string to LPVOID" to "it does not show any data".
Who is "it"? And I guess that the problem is not in LPVOID, but in your function.

To Pallini English is not my native language... and I'm not strong in English. maybe I cant explain you a simple thing... but... after all of this I think, that there is no need to explain you something... I just tell you what said the great Johann Wolfgang von Goethe Все дypаки - yпpямые, и все yпpямые - дypаки. Sorry for my NOT english, but I don't know the translation of this aphorism.
 
Share this answer
 
v2
One more time. your strData's value changes dynamically as you said. So I think the problem is in strData. It gets an ampty string before calling function WinHttpSendRequest.

To pallini I'm not yoga, is that difficult to you to understand? see what said Johann Wolfgang von Goethe.
It'll be better if you let your answer's first revision... I don't think, that anybody will vote that "answer" more than 1
 
Share this answer
 
v2
hey... "buddies"... somebody tell the guy WHY function
<br />
bool AsyncWinHttp::SendRequest(LPWSTR szURL,LPVOID lpBuffer,char strRequest[4])<br />

was not working but this one
<br />
bool AsyncWinHttp::SendRequest(LPWSTR szURL,LPSTR lpBuffer,char strRequest[4])<br />

works fine!

I've already tried to do that... but avika (as well as every voter) didn't pay attention to my post.. Or they found that I am wrong.

Richard... don't you think that mentors should revise the behavior of some posters??? maybe YOU will explain "buddy" why his code was not working?
 
Share this answer
 
Why not change your function definition as below
bool AsyncWinHttp::SendRequest(LPWSTR szURL,LPVOID lpBuffer,char strRequest[4])


// change to this
bool AsyncWinHttp::SendRequest(LPWSTR szURL,LPSTR lpBuffer,char strRequest[4])

then change your call to:
strData = "some data";
http.SendRequest("URL", strData.c_str(), "POST");

and in your 'real' SendRequest call you just use a cast thus:
WinHttpSendRequest(szReq, szHeaders, -1L, (LPVOID)lpBuffer, ...
 
Share this answer
 
What is the purpose of such a 'conversion'?

To Jast_in: I made the question to get context info (and possibly give better help). It looks like such info eventually came out.
Klingons don't do yoga. Yoga is for sissies. :-\
Just_in wrote:
if you really want to to help the man you could first of all ask him to post a code, not ask a stupid question with irony

The 'stupid question' outcome is probably helping him. You shouldn't make (wrong) assumptions about my questions: or does yoga THINKING allow it? :rolleyes:
Just_in wrote:
English is not my native language... and I'm not strong in English

Well, and what is your strength, yoga? :-\


To Avikal: could you please post the two different calls to WinHttpSendRequest?
Why while your SendRequest method accepts a LPVOID argument, you are passing the address of an LPVOID?
What happens if you declare lpBuffer static? Namely
static LPVOID lpBuffer = (LPVOID)strData.c_str();

BTW how strData is declared? Is it a member of your class?


To Mr.Univoter(s): make sure you understand, before... Well, words to the wind, I guess... :rolleyes:

Now I have a proof (do we really need it?): at least one of the Mr.Univoters, doesn't understand (but he is not a coward, after all).
:)
 
Share this answer
 
v10
Thanks a lot Richard... issue is resolved, I have done changes in my function according to your suggestion and not it is working fine.

Thanks a lot budy ...
 
Share this answer
 
There where no "windows" taged in the question, so maybe it is impossible to check the memory as _Superman_ said... so you can also check that by converting the lpBuffer to char*:

char* pBuf = (char*)lpBuffer;

pBuf will get back your "Some Data"


to Pallini: maybe the propose is to understand the mechanism... the question is not "is it needless or not?".

One of "Univoters" is me. But I voted _Sperman_'s answer too... And if you can do the first exercise of yoga (MAYBE) you will understand WHY you got "1" but _Superman_ got 5.

hmm... as I remember there where a question about you somewhere... Am I right? If yes - words to the wind...

To Pallini
Maybe you're not "sissy" but I'm almost sure.... you ARE "cissy". And there is no need to be guru in yoga to be able to do "the first exercise of yoga"... "the first exercise of yoga" is THINKING. So by your words you don't want to that think. And... if you really want to to help the man you could first of all ask him to post a code, not ask a stupid question with irony
 
Share this answer
 
v6
it is showing the same data after conversion

char* pBuf = (char*)lpBuffer;

But if I am passing the same lpBuffer to my function WinHttpSendRequest then it does not show any data at that page.

While if I am sending the data like

LPVOID lpBuffer = "Some Data";

then it is showing the data on that page.

Please help me what i am doing wrong.
 
Share this answer
 
I have already tried both lpBuffer and &lpBuffer, in both cases it does not show any data.
 
Share this answer
 
Jast_in wrote:
Richard... don't you think that mentors should revise the behavior of some posters??? maybe YOU will explain "buddy" why his code was not working?


Dealing with the second question first, the issue is with the compiler understanding the use of a pointer. A LPVOID type is effectively a pointer to no type of data, so it is impossible for the compiler/debugger to 'see' what is pointed at; creating an LPVOID and allocating some data to it has no meaning. The reason for casting a LPSTR, or any other type, to LPVOID as in the following line:
WinHttpSendRequest(szReq, szHeaders, -1L, (LPVOID)lpBuffer, ...

is that the definition of WinHttpSendRequest may specify some other type for the fourth parameter (for example BYTE*), so the actual data pointer needs to be cast to some opaque type for the compiler to accept it without generating an error. If my explanation is still not clear I would recommend you read the C++ documentation on pointers and casts.


To your first question I would say this: this is a self-moderating forum, if people behave badly then they will soon find their posts are ignored or down-voted. In extreme(usually very extreme) cases they can be banned from the forum. The philosophy of the forum is that we try to accept that everyone has something useful to say, but sometimes may be misunderstood because of the difficulty with speaking or understanding English. If you feel that someone's comment is not appropriate then ask them to explain, don't just accuse them of being stupid, which is considered an insult. And especially do not post insults/criticism in a foreign language; this is an English language forum, if you would prefer one in your own language then you are free to create it. The forum succeeds because we all try to help one another, but some questioners need to understand that we are here only to help them, not to do their work for them.
 
Share this answer
 
I am using following code to send the data ...

int _tmain(int argc, _TCHAR* argv[])
{
	hEvent = CreateEvent(NULL, false, false, NULL);

	AsyncWinHttp http;
	http.Initialize(WinHttp_CallBack);
	http.SetTimeout(3 * 60 * 1000);

	strData = "Data to be send";
	LPVOID lpBuffer = (LPVOID)strData.c_str();

	http.SendRequest(L"http://localhost:1429/AIS-Abstractor/DataFromAgent/AgentInstallationDetails.aspx",&lpBuffer,"POST");
	
	WaitForSingleObject(hEvent, INFINITE);
	CloseHandle(hEvent);
}

bool AsyncWinHttp::SendRequest(LPWSTR szURL,LPVOID lpBuffer,char strRequest[4])
{
	WCHAR szHost[256];
	DWORD dwOpenRequestFlag = 0;
	URL_COMPONENTS urlComp;
	bool fRet = false;

	// Initialize URL_COMPONENTS structure.
	ZeroMemory(&urlComp, sizeof(urlComp));
	urlComp.dwStructSize = sizeof(urlComp);

	// Use allocated buffer to store the Host Name.
	urlComp.lpszHostName        = szHost;
	urlComp.dwHostNameLength    = sizeof(szHost) / sizeof(szHost[0]);

	// Set non-zero lengths to obtain pointer a to the URL Path.
	// NOTE: If we threat this pointer as a null-terminated string,
	//       it gives us access to additional information as well. 
	urlComp.dwUrlPathLength = -1;

	// Crack HTTP scheme.
	urlComp.dwSchemeLength = -1;

	// Set the szMemo string.
	swprintf_s( memo_, L"WinHttpCrackURL");

	// Crack the URL.
	if (!WinHttpCrackUrl(szURL, 0, 0, &urlComp))
	{
		goto cleanup;
	}

	// Set the szMemo string.
	swprintf_s( memo_, L"WinHttpConnect");

	// Open an HTTP session.
	connect_ = WinHttpConnect(session_, szHost, 
		urlComp.nPort, 0);
	if (NULL == connect_)
	{
		goto cleanup;
	}

	// Prepare OpenRequest flag
	dwOpenRequestFlag = (INTERNET_SCHEME_HTTPS == urlComp.nScheme) ? WINHTTP_FLAG_SECURE : 0;

	// Set the szMemo string.
	swprintf_s( memo_, L"WinHttpOpenRequest");
	
	if(strcmp(strRequest, "GET")== 0)
	{
		// Open a "GET" request.
		request_ = WinHttpOpenRequest(connect_, 
		L"GET", urlComp.lpszUrlPath,
		NULL, WINHTTP_NO_REFERER, 
		WINHTTP_DEFAULT_ACCEPT_TYPES,
		dwOpenRequestFlag);	
	}
	else
	{
		// Open a "POST" request.
		request_ = WinHttpOpenRequest(connect_, 
		L"POST", urlComp.lpszUrlPath,
		NULL, WINHTTP_NO_REFERER, 
		WINHTTP_DEFAULT_ACCEPT_TYPES,
		dwOpenRequestFlag);	
	}

	if (request_ == 0)
	{
		goto cleanup;
	}

	// Set the szMemo string.
	swprintf_s( memo_, L"WinHttpSetStatusCallback");

	// Install the status callback function.
	WINHTTP_STATUS_CALLBACK pCallback = WinHttpSetStatusCallback(request_,
		(WINHTTP_STATUS_CALLBACK)AsyncCallback,
		WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS | 
		WINHTTP_CALLBACK_FLAG_REDIRECT,    
		NULL);

	// note: On success WinHttpSetStatusCallback returns the previously defined callback function.
	// Here it should be NULL
	if (pCallback != NULL)
	{
		goto cleanup;
	}

	// Set the szMemo string.
	swprintf_s( memo_, L"WinHttpSendRequest");

	if(strcmp(strRequest, "GET")== 0)
	{
		// Send the GET request.
		if (!WinHttpSendRequest(request_, 
			WINHTTP_NO_ADDITIONAL_HEADERS, 0, 
			WINHTTP_NO_REQUEST_DATA, 0, 0, 
			(DWORD_PTR)this))
		{
			goto cleanup;
		}	
	}
	else
	{
		// Send the POST request.
		if (!WinHttpSendRequest(request_, 
			_T("Content-Type: application/x-www-form-urlencoded"),
			-1L,lpBuffer,100,100,(DWORD_PTR)this))
		{
			goto cleanup;
		}
	}

	fRet = true;

cleanup:

	if (fRet == false)
	{
		WCHAR szError[256];

		// Set the error message.
		swprintf_s(szError, L"%s failed with error %d", memo_, GetLastError());

		status.set(ASYNC_WINHTTP_ERROR, szError);

		// Cleanup handles.
		Cleanup();

	}

	return fRet;
}
 
Share this answer
 
v2
I don't thinks so problem is in function bcoz it is working fine in other cases where I am sending the static data in the buffer like lpBuffer ="Data to be send"; but when I am trying to send dynamic data which I am getting in string and if trying to send this data then it does not work successfully.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900