Newsgroups: comp.windows.ms.programmer
Path: funic!news.funet.fi!sunic!psinntp!psinntp!sgigate!sgiblab!spool.mu.edu!snorkelwacker.mit.edu!ira.uka.de!gmd.de!jvnc.net!news.edu.tw!tpts1!root
From: idpt167@tpts1.seed.net.tw
Subject: wsprintf() anomaly
Message-ID: <1992Oct30.093553.2061@tpts1.seed.net.tw>
Sender: root@tpts1.seed.net.tw (Operator)
Organization: Seednet Information Service Center
Date: Fri, 30 Oct 1992 09:35:53 GMT
Lines: 78

My supervisor requested me to write a Windows installation program for
our new product.  I did that job by adapting the Windows installation program
accompanying Jeffrey M. Richter's book, "Windows 3: A Developer's Guide,"
because it is the only appropriate source I have.  In addition to copying
files and setting up program group, the adapted installation program also
modifies win.ini by inserting the application's file name -- say, winapp.exe
-- immediately after "run=" keyword under [windows] section.  For example,
if the original win.ini contains the following entry:

[windows]

run=pbrush.exe


then after the installation it should become:

[windows]

run=winapp.exe pbrush.exe


I thought it can be easily done with the following MS-C code:


char	szWinExeFiles[144];  // file names of the start-up Windows apps.

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
		    LPSTR lpszCmdLine, int nCmdShow)	{

	// ... [setup code]

	// Modify WIN.INI
	GetProfileString("windows", "run", "", (LPSTR) szWinExeFiles, 144);
	wsprintf((LPSTR) szWinExeFiles, "winapp.exe %s", (LPSTR) szWinExeFiles);
	WriteProfileString("windows", "run", (LPSTR) szWinExeFiles);

	// ... [display setup success message]
}


But the following win.ini resulted:

[windows]

run=winapp.exe winapp.exe winapp.


I suspect that using the same character-string variable for both source and
destination buffer in the wsprintf() function causes this recursive behavior.
The problem is solved by using different variables for parameters in
wsprintf() as shown below:


char	szOldWinExeFiles[144];  // "run=" .exe file names in the new win.ini
char	szWinExeFiles[144];     // "run=" .exe file names in the old win.ini

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
		    LPSTR lpszCmdLine, int nCmdShow)	{

	// ... [setup code]

	// Modify WIN.INI
	GetProfileString("windows", "run", "", (LPSTR) szOldWinExeFiles, 144);
	wsprintf((LPSTR) szWinExeFiles, "winapp.exe %s", (LPSTR) szOldWinExeFiles);
	WriteProfileString("windows", "run", (LPSTR) szWinExeFiles);

	// ... [display setup success message]
}


Is this a bug in MS-C (I use version 6.0) or a programming mistake of my own?


--
Hsu-Ku "Bruce" Ying                E-Mail: idpt167@tpts1.seed.net.tw
Software Engineer                  TEL: 886-2-2452227 ext. 352
ARMAS Computer Corp.               FAX: 886-2-2452218
2F #9 Lane 327 Sec. 2 Chung-Shan Road, Chung-Ho City, Taipei, Taiwan
