473,408 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Wrong type of char array

Hi,

I am using VS.net 2003 to write a managed C++ windows application.
My main form traps the WM_DEVICECHANGE message and calls a method in my own
unmanaged class passing the wParam and lParam.
In the method, I cast the lParam to a PDEV_BROADCAST_DEVICEINTERFACE like:

pdbcd = (PDEV_BROADCAST_DEVICEINTERFACE) lParam;

The problem I'm having is that the pdbcd->dbcc_name is only one character "\".
If I look at it in memory, I can see what it should be ("\\?\USB#Vid_0
etc...") but it is twice as long and every other character is a 0
(("\.\.?.\.U.S.B.#.V.i.d._.0. etc...")).

I've tried the different Language option settings but they all seem to work
the same. I guess I can live with it as long as I can get the correct value
in after the cast.

Any ideas what I may be doing wrong or ways to fix it?

Thanks,
Joe

Nov 17 '05 #1
5 1919
On Thu, 7 Apr 2005 16:49:02 -0700, Joe Thompson wrote:
Hi,

I am using VS.net 2003 to write a managed C++ windows application.
My main form traps the WM_DEVICECHANGE message and calls a method in my own
unmanaged class passing the wParam and lParam.
In the method, I cast the lParam to a PDEV_BROADCAST_DEVICEINTERFACE like:

pdbcd = (PDEV_BROADCAST_DEVICEINTERFACE) lParam;

The problem I'm having is that the pdbcd->dbcc_name is only one character "\".
If I look at it in memory, I can see what it should be ("\\?\USB#Vid_0
etc...") but it is twice as long and every other character is a 0
(("\.\.?.\.U.S.B.#.V.i.d._.0. etc...")).

I've tried the different Language option settings but they all seem to work
the same. I guess I can live with it as long as I can get the correct value
in after the cast.

Any ideas what I may be doing wrong or ways to fix it?

Thanks,
Joe


The DEV_BROADCAST_DEVICEINTERFACE dbcc_name member is a TCHAR array, which
means it's either char or wchar_t depending on the UNICODE and _UNICODE
macros. In your code, you're observing a one character string, but
inspecting the memory reveals Windows is giving you a Unicode string. This
implies TCHAR is char and that your form has a Unicode window procedure (on
your current system, at least) irrespective of these macros. You'll have to
account for this, and while I haven't done this myself, here are some
things that come to mind.

If you don't care about Win9X, you can just build for Unicode, but if you
want to be portable to Win9X, you'll have to build a separate ANSI version
(unless you want to use MSLU). This will let you use things defined in
terms of TCHAR directly. (You should prevent an ANSI version from running
on NT-based Windows.)

You can detect at runtime if your window procedure is Unicode by using
IsWindowUnicode, but probably inspecting Marshal.SystemDefaultCharSize
would be good enough. Then you can cast to PDEV_BROADCAST_DEVICEINTERFACE_A
or PDEV_BROADCAST_DEVICEINTERFACE_W as appropriate.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2
Hi Doug,

Thanks for the reply. I tried casting it to each one of the two (_W and _A)
but it doesn't like the _W. When I set the project to be Unicode, I get a
whole bunch of other errors. I was given example code from a vendor that was
written in straight C. Their sample app compiles and runs fine.
Unfortunately, I am more interested in making the GUI part so would like to
use .NET winforms. I think the problem is stemming from mixing in the
straigth C. It doesn't like function pointers from my managed C++, etc.

I will try digging in a little deeper.

Thanks again,
Joe

"Doug Harrison [MVP]" wrote:
On Thu, 7 Apr 2005 16:49:02 -0700, Joe Thompson wrote:
Hi,

I am using VS.net 2003 to write a managed C++ windows application.
My main form traps the WM_DEVICECHANGE message and calls a method in my own
unmanaged class passing the wParam and lParam.
In the method, I cast the lParam to a PDEV_BROADCAST_DEVICEINTERFACE like:

pdbcd = (PDEV_BROADCAST_DEVICEINTERFACE) lParam;

The problem I'm having is that the pdbcd->dbcc_name is only one character "\".
If I look at it in memory, I can see what it should be ("\\?\USB#Vid_0
etc...") but it is twice as long and every other character is a 0
(("\.\.?.\.U.S.B.#.V.i.d._.0. etc...")).

I've tried the different Language option settings but they all seem to work
the same. I guess I can live with it as long as I can get the correct value
in after the cast.

Any ideas what I may be doing wrong or ways to fix it?

Thanks,
Joe


The DEV_BROADCAST_DEVICEINTERFACE dbcc_name member is a TCHAR array, which
means it's either char or wchar_t depending on the UNICODE and _UNICODE
macros. In your code, you're observing a one character string, but
inspecting the memory reveals Windows is giving you a Unicode string. This
implies TCHAR is char and that your form has a Unicode window procedure (on
your current system, at least) irrespective of these macros. You'll have to
account for this, and while I haven't done this myself, here are some
things that come to mind.

If you don't care about Win9X, you can just build for Unicode, but if you
want to be portable to Win9X, you'll have to build a separate ANSI version
(unless you want to use MSLU). This will let you use things defined in
terms of TCHAR directly. (You should prevent an ANSI version from running
on NT-based Windows.)

You can detect at runtime if your window procedure is Unicode by using
IsWindowUnicode, but probably inspecting Marshal.SystemDefaultCharSize
would be good enough. Then you can cast to PDEV_BROADCAST_DEVICEINTERFACE_A
or PDEV_BROADCAST_DEVICEINTERFACE_W as appropriate.

--
Doug Harrison
Microsoft MVP - Visual C++

Nov 17 '05 #3
On Fri, 8 Apr 2005 12:45:03 -0700, Joe Thompson wrote:
Hi Doug,

Thanks for the reply. I tried casting it to each one of the two (_W and _A)
but it doesn't like the _W.
What error message do you get?
When I set the project to be Unicode, I get a
whole bunch of other errors. I was given example code from a vendor that was
written in straight C. Their sample app compiles and runs fine.
It probably wasn't written to be "TCHAR correct".
Unfortunately, I am more interested in making the GUI part so would like to
use .NET winforms. I think the problem is stemming from mixing in the
straigth C. It doesn't like function pointers from my managed C++, etc.


The C compiler does not do managed code, but as long as you're compiling
your "straight C" as C++, you should be able to take advantage of the IJW
feature. Can't say anything about function pointers without understanding
what you're doing.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #4

Hi Thompson,

I am facing the exact same problem.
I am developing the GUI in C# and have a c++ .NET DLL.
I'm am also getting pdbcd->dbcc_name as only one character "\".
I would like to know if you have the solution to this?
I would appreciate the help.

Thanks
-M
Joe Thompson wrote:
*Hi,

I am using VS.net 2003 to write a managed C++ windows application.
My main form traps the WM_DEVICECHANGE message and calls a method in
my own
unmanaged class passing the wParam and lParam.
In the method, I cast the lParam to a PDEV_BROADCAST_DEVICEINTERFACE
like:

pdbcd = (PDEV_BROADCAST_DEVICEINTERFACE) lParam;

The problem I'm having is that the pdbcd->dbcc_name is only one
character "\".
If I look at it in memory, I can see what it should be
("\\?\USB#Vid_0
etc...") but it is twice as long and every other character is a 0
(("\.\.?.\.U.S.B.#.V.i.d._.0. etc...")).

I've tried the different Language option settings but they all seem
to work
the same. I guess I can live with it as long as I can get the
correct value
in after the cast.

Any ideas what I may be doing wrong or ways to fix it?

Thanks,
Joe *


--
mayuravb
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Feb 3 '06 #5
It sounds to me like the string in memory is Unicode, i.e. has 16 bit
chars, and you are dereferencing it as a null-terminated string of
standard C++ (i.e. 8 bit) chars. Try treating it as an array of wide
characters instead.

-Ted

mayuravb wrote:
Hi Thompson,

I am facing the exact same problem.
I am developing the GUI in C# and have a c++ .NET DLL.
I'm am also getting pdbcd->dbcc_name as only one character "\".
I would like to know if you have the solution to this?
I would appreciate the help.

Thanks
-M
Joe Thompson wrote:
*Hi,

I am using VS.net 2003 to write a managed C++ windows application.
My main form traps the WM_DEVICECHANGE message and calls a method in
my own
unmanaged class passing the wParam and lParam.
In the method, I cast the lParam to a PDEV_BROADCAST_DEVICEINTERFACE
like:

pdbcd = (PDEV_BROADCAST_DEVICEINTERFACE) lParam;

The problem I'm having is that the pdbcd->dbcc_name is only one
character "\".
If I look at it in memory, I can see what it should be
("\\?\USB#Vid_0
etc...") but it is twice as long and every other character is a 0
(("\.\.?.\.U.S.B.#.V.i.d._.0. etc...")).

I've tried the different Language option settings but they all seem
to work
the same. I guess I can live with it as long as I can get the
correct value
in after the cast.

Any ideas what I may be doing wrong or ways to fix it?

Thanks,
Joe *


--
mayuravb
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Feb 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: David. E. Goble | last post by:
Hi all; I have the following files; index.html, sigsheader.js, sigsboby.js, smilesbody.js and smiles.js. The sourse is below. The page displays two manual slide shows... Each slideshow has a set...
2
by: Abby | last post by:
I need to do 8 bits 2's complement checksum, so I wrote code in order to see if the checksum calculation is correct. ===========================================================================...
9
by: David. E. Goble | last post by:
Arrrh! some buttons work while others don't, but I can't see why. I have tried comparing the files that do work, with the ones that don't. But to no help. The funny thing is the parts that work...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
7
by: Michael | last post by:
Hi, I am trying to delete p to see whether the thing pointed toby p is deleted. However, the program is stuck there. Could you please help me out? Thanks in advance, Michael #include...
30
by: Bill Reid | last post by:
#define MAX_VALUES 64 typedef struct { unsigned value_1; double value_2; double value_3; double value_4; } VALUES; typedef struct {
2
by: linq936 | last post by:
Hi, I have a typedef like this, typedef (char* ) arrp; I want to define a type which is an array, the array has 20 elements each of which is a char*. I get compile error:
3
by: Virtual_X | last post by:
char *x = {"abc","eqx",.....}; this is valid and the same in int in not valid int *x = {1,2,3,4,....}; -------------------------------------------------------------- and also char *seg=...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.