Wrong type of char array

G

Guest

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
 
D

Doug Harrison [MVP]

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.
 
G

Guest

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
 
D

Doug Harrison [MVP]

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.
 
M

mayuravb

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
 
T

Ted Williams

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top