|
Zac Howland wrote: That is one of the topics that should be required
one of the "many" that aren't.
led mike
|
|
|
|
|
here's the sample code:
<br />
BOOL IsValid(MyType *p)<br />
{<br />
<br />
<br />
.....<br />
}<br />
<br />
void TestIsValid ()<br />
{<br />
MyType a;<br />
BOOL bRet;<br />
<br />
bRet = IsValid (&a);<br />
printf ("IsValid( ) returns %d.\n", bRet);<br />
}<br />
So I don't see I can use what you recommended first as:
<br />
BOOL IsValid (MyType *p)<br />
{<br />
...<br />
#ifdef _DEBUG<br />
TestIsValid();<br />
#endif<br />
}<br />
I hope I made myself clear this time.
|
|
|
|
|
So place it in the other function:
void TestIsValid()
{
MyType a;
BOOL bRet = false;
#ifdef _DEBUG
bRet = IsValid (&a);
printf ("IsValid( ) returns %d.\n", bRet);
#endif
}
|
|
|
|
|
What with ANSI, UNICODE and MBCS there's a lot can go wrong. Usualy I build MBCS only apps, but I am now taking into account that I may one day re-use my code in a UNICODE build. So I am making use of tchar.h .
Now, I have created a function which performs various tasks on a string
int DrawVertText(HDC hdc,LPCTSTR lpString,int nCount,LPRECT lpRect,UINT uFormat)
According to MSDN with a MBCS build, that LPCTSTR Resolves to a "constant string of TCHAR (const TCHAR*)" which in turn resolves to a "constant string of char (const char*)", and for a UNICODE build a "constant string of wchar_t (const wchar_t*)". I later use the string
int iLength = _tcsclen(lpString); In my MBCS build it works, when I switch to UNICODE I get the following error
error C2664: 'wcslen' : cannot convert parameter 1 from 'const char *' to 'const unsigned short *'
Why has the string not been resolved to wchar_t ? Also, the mouseover popup in MSVS tells me that lpString is an unsigned short* .
|
|
|
|
|
A simple solution to a simple problem. In my stdafx.h I added
#ifdef _UNICODE
#ifndef UNICODE
#define UNICODE
#endif
#endif
Why are there two definitions?
|
|
|
|
|
waldermort wrote: Why are there two definitions?
The simple answer: When you have multiple teams working on adding support for similar features, and they don't communicate well, you end up with overlapping functionality.
This is also part of the reason why there are sections of MFC that deal with return values for errors, others that throw exceptions, and still others that require you to check GetLastError periodically.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
UNICODE is used by the Windows headers to switch between the A and W versions of APIs
_UNICODE is used by the CRT headers to switch between MBCS and Unicode versions of the string functions (this is how _tcsxxx macros work)
You should always define both or neither of those symbols
--Mike--
Visual C++ MVP
LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
|
|
|
|
|
I want to add the regsvr32 command as a post-build event in my makefile.How do i do it?
Is it possible to specify post-build events in makefiles?
a small example will be appreciated
|
|
|
|
|
Hello,
I have a program that takes a command line argument and then converts it to a unicode string (with MultiByteToWide) for use in NetLocalGroupEnum (not shown in the program extract below).
The strange thing is that when I have a printf (line 18 below) at the start of the program, it seems to work fine (It prints the converted string).
But when I remove the printf the string conversion is not correct anymore. I must be doing something wrong with 'sizeof' or with the MultiByteToWideChar function.
01 #pragma comment( lib, "netapi32.lib" )
02 #include <stdio.h>
03
04 #include <stdlib.h>
05 //#include <string.h>
06 #include <windows.h>
07 #include <atl\atlbase.h>
08
09 #include <lmcons.h>
10 #include <lmaccess.h>
11 #include <lm.h>
12 #include <sddl.h>
13
14 int main(int argc, char** argv)
15 {
16 char buf[256];
17
18 //printf("Test\n\n");
19
20 if (argc < 2) {
21 printf("Usage:lgenum2 <servername>");
22 exit(EXIT_FAILURE);
23 }
24
25 LPWSTR servername = new WCHAR[sizeof(argv[1])+1];
26
27 MultiByteToWideChar (
28 CP_ACP, // code page
29 0, // performance and mapping flags
30 (char *) argv[1], // address of character string
31 -1, // number of characters in string
32 servername, // address of buffer for new string
33 wcslen(servername) // size of buffer
34 );
35
36 wprintf(L"Multibyte to Widechar:%s\n",servername);
37 printf("-------\n\n");
38 exit(EXIT_SUCCESS);
39 }
Thanks for any help
Francis
|
|
|
|
|
fvandun wrote: LPWSTR servername = new WCHAR[sizeof(argv[1])+1];
26
27 MultiByteToWideChar (
28 CP_ACP, // code page
29 0, // performance and mapping flags
30 (char *) argv[1], // address of character string
31 -1, // number of characters in string
32 servername, // address of buffer for new string
33 wcslen(servername) // size of buffer
34 );
The above should be written as:
size_t wStrSize = strlen(argv[1] + 1);
LPWSTR servername = new WCHAR[wStrSize];
MultiByteToWideChar( CP_ACP,
MB_PRECOMPOSED,
(const char*)argv[1],
-1,
servername,
wStrSize);
That should fix your problem.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Thanks Zac,
I substituted your lines.
But now when I print my MultiByte string with wprintf, it has a lot of garbage characters at the end:
D:\Dev\CPP\Projects>t1 01234567890123456789
Test 01234567890123456789
Multibyte to Widechar:01234567890123456789▄♠n\?2?2???????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????????---------------------------------------
Any more suggestions?
Thanks
Francis
|
|
|
|
|
Add a call to memset after the WCHAR string is created:
LPWSTR servername = new WCHAR[wStrSize];
memset(servername, 0, sizeof(WCHAR) * wStrSize);
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Still no luck.
With printf statement on line 17, I get the garbage at the end:
D:\Dev\CPP\Projects>t1 0123456789012345678901234567890
Multibyte to Widechar:012345678901234567890123456789??+ s6?
...
???????????????????????????????????????\???????????????????
---------------------------------------
Without printf statement on line 17: OK
D:\Dev\CPP\Projects>t1 0123456789012345678901234567890
Multibyte to Widechar:012345678901234567890123456789
---------------------------------------
01 #pragma comment( lib, "netapi32.lib" )
02 #include <stdio.h>
03
04 #include <stdlib.h>
05 //#include <string.h>
06 #include <windows.h>
07 #include <atl\atlbase.h>
08 #include <wchar.h>
09 #include <lmcons.h>
10 #include <lmaccess.h>
11 #include <lm.h>
12 #include <sddl.h>
13
14 int main(int argc, char** argv)
15 {
16 if (argc < 2) {
17 printf("Usage:lgenum2 <servername>");
18 exit(EXIT_FAILURE);
19 }
20 size_t wStrSize = strlen(argv[1]+1);
21 //printf("");
22 //printf("Test %s,%u\n", argv[1],wStrSize);
23
24 LPWSTR servername = new WCHAR[wStrSize];
25 wmemset(servername, 0, wStrSize);
26 MultiByteToWideChar(
27 CP_ACP, // code page
28 MB_PRECOMPOSED, // performance and mapping flags
29 (const char*)argv[1], // address of character string
30 -1, // number of characters in string
31 servername, // address of buffer for new string
32 wStrSize); // size of buffer
33
36 wprintf(L"Multibyte to Widechar:%s\n",servername);
37 wprintf(L"---------------------------------------\n");
38 exit(EXIT_SUCCESS);
39 }
For completeness, here are my VC Express environment settings:
DevEnvDir=D:\Progs\VStudio8\Common7\IDE
INCLUDE=D:\Progs\VStudio8\VC\INCLUDE;D:\Progs\MsPSDK2003R2\Include;D:\Progs\MsPSDK2003R2\Include\atl;C:\sybase\OCS-12_5\include;
LIB=D:\Progs\VStudio8\VC\LIB;D:\Progs\VStudio8\SDK\v2.0\lib;D:\Progs\MsPSDK2003R2\Lib;C:\sybase\OCS-12_5\lib;
LIBPATH=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
Path=D:\Progs\VStudio8\Common7\IDE;D:\Progs\VStudio8\VC\BIN;D:\Progs\VStudio8\Common7\Tools;D:\Progs\VStudio8\SDK\v2.0\bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;D:\Progs\VStudio8\VC\VCPackages;D:\Progs\EMC/lib;D:\Progs\EMC/bin;D:\Python24\.;C:\Program Files\Windows Resource Kits\Tools\;D:\oracle\product\10.2.0\client_1;D:\VERITAS\NetBackup\bin\;C:\qip;C:\qip\utils;C:\qip\lib;C:\sybase\CFG-1_0\bin;C:\sybase\OCS-12_5\dll;C:\sybase\OCS-12_5\lib3p;C:\sybase\OCS-12_5\bin;D:\Perl\bin\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI Control Panel;C:\Program Files\Common Files\Adaptec Shared\System;C:\Program Files\UltraEdit;D:\usr\bin;D:\usr\32-bit;D:\usr\32-bit\utils;C:\Program Files\NMapWin\\bin;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;D:\usr\reskit;C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin;C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE;D:\Progs\ZipGenius 6\;D:\usr\bin;D:\Progs\EMC\VISUAL~1;C:\PROGRA~1\EMC\EMCVIS~1;C:\Program Files\UltraEdit;c:\qip\cli;D:\Progs\EMC/lib;D:\Progs\EMC/bin;D:\Python24\.;C:\Program Files\Windows Resource Kits\Tools\;D:\oracle\product\10.2.0\client_1;D:\VERITAS\NetBackup\bin\;C:\qip;C:\qip\utils;C:\qip\lib;C:\sybase\CFG-1_0\bin;C:\sybase\OCS-12_5\dll;C:\sybase\OCS-12_5\lib3p;C:\sybase\OCS-12_5\bin;D:\Perl\bin\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI Control Panel;C:\Program Files\Common Files\Adaptec Shared\System;C:\Program Files\UltraEdit;D:\usr\bin;D:\usr\32-bit;D:\usr\32-bit\utils;C:\Program Files\NMapWin\\bin;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;D:\usr\reskit;C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin;C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE;D:\Progs\ZipGenius 6\;D:\usr\bin;D:\Progs\EMC\VISUAL~1;C:\PROGRA~1\EMC\EMCVIS~1
VCINSTALLDIR=D:\Progs\VStudio8\VC
VS80COMNTOOLS=D:\Progs\VStudio8\Common7\Tools\
VSINSTALLDIR=D:\Progs\VStudio8
|
|
|
|
|
Do'h! Part of it was a typo on my part:
size_t wStrSize = strlen(argv[1] + 1);
should read as:
size_t wStrSize = strlen(argv[1]) + 1;
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Thanks!
That did it.
Francis
|
|
|
|
|
Hi,
Can a namespace extension be installed on a limited users account? To install one only accesses the HKEY_CURRENT_USER part of the registry (as far as I remember) so does this mean that they can be installed?
Thanks in advance
Dave Kerr
codechamber@hotmail.com
http://www.codechamber.com
|
|
|
|
|
Hi there guys, I am currently trying to achieve a seemingly simple task in VC++ 2005. I have made a very simple form in Microsoft Access which I wish to serve as the beginnings of something greater. I created a db in MS Access named links.mdb containing on table-> Table1. Table1 contains 1 column, "Links", and i wish to read these strings into variables in my Visual C++ Windows Forms Application.
What I have done so far...
In Visual C++, I clicked on Data->Add new data source, and followed the wizard to add the microsoft access database to my application by the name, "linksDataSet". I can see the table in my left hand "Data Sources" pane in VC++. All I need to know is how to access my database from here so that I can read these strings stored in my table. Also, would be possible to schedule my application to log on to a http server and retrieve these links every time the application is executed? How would I go about doing this?
Thank you very much for your time
Regards
Linden.
|
|
|
|
|
the sounds like you are using managed code - are you posting in the correct forum or are really using VC++/MFC?
cje
|
|
|
|
|
Hi
Can anyone tell me the best way to obtain the windows directory and drive
thanks
Simon
|
|
|
|
|
Look up SHGetFolderPath in the documentation and use the flag 'CSIDL_WINDOWS', this will get you the path to the windows folder (typically c:\windows).
Always use SHGetFolderPath for special folders like My Documents, Program Files and so on. It's a bit of an ugly function so I'd wrap it in something a bit cleaner that returns a string.
Dave Kerr
codechamber@hotmail.com
http://www.codechamber.com
|
|
|
|
|
|
|
UINT GetWindowsDirectory(
LPTSTR lpBuffer, // buffer for Windows directory
UINT uSize // size of directory buffer
);
G_S
|
|
|
|
|
Hi:
Does anybody know of a good tutorial or getting started resource for using the "Spy++" tool that comes with Visual Studio?
I have a problem with a MFC application which I suspect may be traced if I can look at the Windows messages being shot through my application. However, every time I fire up Spy++, I end up throwing up my hands in dispare.
Specifically, I would like to know how to identify which window I should be watching from the enormous list of windows that are listed in the "Windows" window. Its not obvious and I often find that the name of my application appears embedded in three or more of the entries in the listing. Then, I would like to know how to isolate messages that are being directed to specific windows and possibly even narrow this to specific controls.
Do people find "Spy++" useful? Or are there better ways to debug problems?
Thank you,
Mark
|
|
|
|
|
Spy++ is a great tool
First of all start the application directly meaning not executing it through VC++.
Second a better approach is to list processes it is simply easier to find your exes name then open up the tree you'll see all the threads if mulitple scan the threads for the window/control your looking for
right click the window/control you want and click messages you'll see messages that window/control receives to isolate messages click on logging options on the toolbar or just click CTL+O
there you have what kind of message tracing you want.
G_S
|
|
|
|