home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / os / mswindo / programm / misc / 3263 < prev    next >
Encoding:
Text File  |  1992-11-07  |  2.7 KB  |  89 lines

  1. Newsgroups: comp.os.ms-windows.programmer.misc
  2. Path: sparky!uunet!spool.mu.edu!yale.edu!jvnc.net!news.edu.tw!tpts1!root
  3. From: idpt167@tpts1.seed.net.tw
  4. Subject: wsprintf() anomaly
  5. Message-ID: <1992Oct30.093046.1778@tpts1.seed.net.tw>
  6. Sender: root@tpts1.seed.net.tw (Operator)
  7. Organization: Seednet Information Service Center
  8. Date: Fri, 30 Oct 1992 09:30:46 GMT
  9. Lines: 78
  10.  
  11. My supervisor requested me to write a Windows installation program for
  12. our new product.  I did that job by adapting the Windows installation program
  13. accompanying Jeffrey M. Richter's book, "Windows 3: A Developer's Guide,"
  14. because it is the only appropriate source I have.  In addition to copying
  15. files and setting up program group, the adapted installation program also
  16. modifies win.ini by inserting the application's file name -- say, winapp.exe
  17. -- immediately after "run=" keyword under [windows] section.  For example,
  18. if the original win.ini contains the following entry:
  19.  
  20. [windows]
  21.  
  22. run=pbrush.exe
  23.  
  24.  
  25. then after the installation it should become:
  26.  
  27. [windows]
  28.  
  29. run=winapp.exe pbrush.exe
  30.  
  31.  
  32. I thought it can be easily done with the following MS-C code:
  33.  
  34.  
  35. char    szWinExeFiles[144];  // file names of the start-up Windows apps.
  36.  
  37. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  38.             LPSTR lpszCmdLine, int nCmdShow)    {
  39.  
  40.     // ... [setup code]
  41.  
  42.     // Modify WIN.INI
  43.     GetProfileString("windows", "run", "", (LPSTR) szWinExeFiles, 144);
  44.     wsprintf((LPSTR) szWinExeFiles, "winapp.exe %s", (LPSTR) szWinExeFiles);
  45.     WriteProfileString("windows", "run", (LPSTR) szWinExeFiles);
  46.  
  47.     // ... [display setup success message]
  48. }
  49.  
  50.  
  51. But the following win.ini resulted:
  52.  
  53. [windows]
  54.  
  55. run=winapp.exe winapp.exe winapp.
  56.  
  57.  
  58. I suspect that using the same character-string variable for both source and
  59. destination buffer in the wsprintf() function causes this recursive behavior.
  60. The problem is solved by using different variables for parameters in
  61. wsprintf() as shown below:
  62.  
  63.  
  64. char    szOldWinExeFiles[144];  // "run=" .exe file names in the new win.ini
  65. char    szWinExeFiles[144];     // "run=" .exe file names in the old win.ini
  66.  
  67. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  68.             LPSTR lpszCmdLine, int nCmdShow)    {
  69.  
  70.     // ... [setup code]
  71.  
  72.     // Modify WIN.INI
  73.     GetProfileString("windows", "run", "", (LPSTR) szOldWinExeFiles, 144);
  74.     wsprintf((LPSTR) szWinExeFiles, "winapp.exe %s", (LPSTR) szOldWinExeFiles);
  75.     WriteProfileString("windows", "run", (LPSTR) szWinExeFiles);
  76.  
  77.     // ... [display setup success message]
  78. }
  79.  
  80.  
  81. Is this a bug in MS-C (I use version 6.0) or a programming mistake of my own?
  82.  
  83.  
  84. --
  85. Hsu-Ku "Bruce" Ying                E-Mail: idpt167@tpts1.seed.net.tw
  86. Software Engineer                  TEL: 886-2-2452227 ext. 352
  87. ARMAS Computer Corp.               FAX: 886-2-2452218
  88. 2F #9 Lane 327 Sec. 2 Chung-Shan Road, Chung-Ho City, Taipei, Taiwan
  89.