Click here to Skip to main content
11,485,334 members (79,736 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 16-Feb-10 22:21pm
avika411
Edited 16-Feb-10 22:42pm
CPallini340.8K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Where do you look for "any data in lpBuffer"? Just look at memory for address of your lpBuffer. It got your "Some Data" Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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:


string strData = "Some Data";
LPVOID lpBuffer = (LPVOID)strData.c_str();


lpBuffer gets the value "Some data"


strData = "another data";


lpBuffer gets the value "another data"

Debug will help you... pay attention to WHERE the value of "strData" is changed!
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

You should pass to "SendRequest" not "&lpBuffer" but lpBuffer
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 11

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 13

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 16

hey... "buddies"... somebody tell the guy WHY function

bool AsyncWinHttp::SendRequest(LPWSTR szURL,LPVOID lpBuffer,char strRequest[4])

was not working but this one

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

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?
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 14

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, ...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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. Shucks | :-\
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? Roll eyes | :rolleyes:
Just_in wrote:
English is not my native language... and I'm not strong in English

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


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... Roll eyes | :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).
Smile | :)
  Permalink  
v10
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 15

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 ...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
v6
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 10

I have already tried both lpBuffer and &lpBuffer, in both cases it does not show any data.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 17

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

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;
}
 
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 12

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 380
1 Maciej Los 365
2 Mehdi Gholam 160
3 Raje_ 145
4 Frankie-C 134
0 Sergey Alexandrovich Kryukov 8,819
1 OriginalGriff 7,982
2 Sascha Lefèvre 3,199
3 Maciej Los 2,841
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 18 Feb 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100