Click here to Skip to main content
15,910,009 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Runtime error for OpenFileDialog Class in .NET 2005 Pin
Stephen Hewitt13-May-07 18:52
Stephen Hewitt13-May-07 18:52 
AnswerRe: Runtime error for OpenFileDialog Class in .NET 2005 Pin
Stephen Hewitt13-May-07 18:57
Stephen Hewitt13-May-07 18:57 
QuestionATL::CImage using HDC ??? Pin
guybour200613-May-07 4:33
guybour200613-May-07 4:33 
AnswerRe: ATL::CImage using HDC ??? Pin
Mark Salsbery13-May-07 5:45
Mark Salsbery13-May-07 5:45 
AnswerRe: ATL::CImage using HDC ??? Pin
bob1697213-May-07 7:46
bob1697213-May-07 7:46 
QuestionStrange strcpy problem Pin
Axonn Echysttas13-May-07 2:17
Axonn Echysttas13-May-07 2:17 
AnswerRe: Strange strcpy problem Pin
Gary R. Wheeler13-May-07 2:31
Gary R. Wheeler13-May-07 2:31 
GeneralRe: Strange strcpy problem Pin
Axonn Echysttas13-May-07 3:09
Axonn Echysttas13-May-07 3:09 
This is really weird. Watch this! I will give you the relevant pieces of code where cTemp and cBuffer are involved. This is really strange for me.

<br />
<br />
char cTemp[260]; //For temporary stringwork.<br />
char cTemp2[260]; //WTF???<br />
char cBuffer[256]; //To read data from the file.<br />
<br />
//This is where I save something into cTemp.<br />
GetModuleFileName(0, cTemp, 249);<br />
<br />
//And this is where I save something into cBuffer.<br />
ReadFile(hFile, cBuffer, sizeof(char)*256, &dwNumRead, NULL);<br />
<br />
strcpy(cTemp2, cBuffer); //Crashes.<br />
strcpy(cTemp, cBuffer); //Also crashes.<br />
<br />


HOWEVER!!! If I move the declaration for cTemp2 straight before the strcpy, it works!!! Like this:

<br />
char cTemp2[260]; //WTF???	<br />
strcpy(cTemp2, cBuffer); //It works!!!.<br />
sTemp = cTemp2;<br />


Why does it work !?!?!?!?!? What does anything have to do with where I declare the variable !??!!?

I'll give you the full code, maybe there's something there I missed. There are some things here which you won't understand, but try not to get stuck in my local program logic. ASDANSIString is a class I made to work easier with strings. It's a wrapper over a char*, quite similar to the String class in .Net, it has lots of parsing methods incorporated in it.

<br />
<br />
//Entry point.<br />
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)<br />
{<br />
	ASDANSIString sTemp; //For temporary stringwork.<br />
	HANDLE hFile; //The file to read the window title from.<br />
	MSG msgPump; //For dispatching OS messages.<br />
	DWORD dwNumRead; //Number of bytes read from the file.<br />
	long lCounter;<br />
	char cTemp[260]; //For temporary stringwork.<br />
	<br />
	char cBuffer[256]; //To read data from the file.<br />
<br />
	bReady = false; //Initializing.<br />
	lTimerID = SetTimer(0, 0, 1000, (TIMERPROC)TimerProc); //Launching timer.<br />
<br />
	//Retrieving the name of the window to close from the file..<br />
	GetModuleFileName(0, cTemp, 249); //Getting this module's path (the executable full name and path).<br />
	sTemp = cTemp; //From now on using the ASDANSIString object to work with the path.<br />
	sTemp = sTemp.SubStringA(0, sTemp.IndexOfRev("\\", 0)); //Copying only until the last backslash.<br />
	sTemp += "\\Window Name.txt"; //Adding the name of the file to open.<br />
<br />
	//Opening the file which contains the window name and reading the window name from it.<br />
	hFile = CreateFile(sTemp, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);	<br />
	ReadFile(hFile, cBuffer, sizeof(char)*256, &dwNumRead, NULL);<br />
	CloseHandle(hFile);<br />
<br />
	char cTemp2[260]; //WTF???<br />
	strcpy(cTemp2, cBuffer);<br />
	sTemp = cTemp2;<br />
<br />
	MessageBox(0, sTemp, "Test", 0);<br />
<br />
 	while (!bReady && GetMessage(&msgPump, NULL, 0, 0)) //Pumping messages, but only while the timer is still active.<br />
	{<br />
		TranslateMessage(&msgPump);<br />
		DispatchMessage(&msgPump);<br />
	}<br />
<br />
 	return 0;<br />
}<br />
<br />


-= E C H Y S T T A S =-
The Greater Mind Balance

Blending C++ with COM ^

GeneralRe: Strange strcpy problem Pin
Hans Dietrich13-May-07 3:47
mentorHans Dietrich13-May-07 3:47 
GeneralRe: Strange strcpy problem Pin
Axonn Echysttas13-May-07 4:17
Axonn Echysttas13-May-07 4:17 
GeneralRe: Strange strcpy problem Pin
Gary R. Wheeler13-May-07 4:32
Gary R. Wheeler13-May-07 4:32 
AnswerRe: Strange strcpy problem Pin
Michael Dunn13-May-07 6:18
sitebuilderMichael Dunn13-May-07 6:18 
QuestionMapping a drive into memory. Pin
Killer22512-May-07 23:23
Killer22512-May-07 23:23 
AnswerRe: Mapping a drive into memory. Pin
Hans Dietrich13-May-07 0:10
mentorHans Dietrich13-May-07 0:10 
AnswerRe: Mapping a drive into memory. Pin
Michael Dunn13-May-07 6:20
sitebuilderMichael Dunn13-May-07 6:20 
Questionvc++ related to xml Pin
biswajit nayak12-May-07 21:46
biswajit nayak12-May-07 21:46 
AnswerRe: vc++ related to xml Pin
Cedric Moonen12-May-07 22:01
Cedric Moonen12-May-07 22:01 
GeneralRe: vc++ related to xml Pin
Hans Dietrich13-May-07 0:12
mentorHans Dietrich13-May-07 0:12 
AnswerRe: vc++ related to xml Pin
Hamid_RT13-May-07 1:21
Hamid_RT13-May-07 1:21 
AnswerRe: vc++ related to xml Pin
#realJSOP13-May-07 3:57
professional#realJSOP13-May-07 3:57 
QuestionOne line miss problem Pin
hsuch12-May-07 19:26
hsuch12-May-07 19:26 
QuestionRe: One line miss problem Pin
bob1697212-May-07 20:06
bob1697212-May-07 20:06 
AnswerRe: One line miss problem Pin
hsuch12-May-07 20:49
hsuch12-May-07 20:49 
GeneralRe: One line miss problem Pin
bob1697212-May-07 23:29
bob1697212-May-07 23:29 
GeneralRe: One line miss problem Pin
hsuch13-May-07 7:43
hsuch13-May-07 7:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.