470,810 Members | 1,148 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

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 1817
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by David. E. Goble | last post: by
2 posts views Thread by Abby | last post: by
2 posts views Thread by leo2100 | last post: by
8 posts views Thread by Martin Jřrgensen | last post: by
7 posts views Thread by Michael | last post: by
30 posts views Thread by Bill Reid | last post: by
2 posts views Thread by linq936 | last post: by
3 posts views Thread by Virtual_X | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.