By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,759 Members | 1,273 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,759 IT Pros & Developers. It's quick & easy.

Com interop: arrays with NULLS truncating when returned to vb.net

P: n/a
I am calling a VB6 dll from a vb.net windows application that returns an array of strings. My issue is it seems to truncate after a NULL character. For Example VB 6 is returning a string with the HEX value of 4E 31 00 00 01 00 20 20 20 20 20 00 00 00 20 20 20 31 32 30. But when it gets back to Vb.net all I have is 4E 31 or N1. Now I can return the same string as a return value of a function and I get it all.

Please Help,

Patrick Horn

---
Posted using Wimdows.net NntpNews Component -

Post Made from http://www.DotNetJunkies.com/newsgroups Our newsgroup engine supports Post Alerts, Ratings, and Searching.
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,

The showing of a string stops when there is a 00 byte in it.
However, normaly it is there in its full lenght and you can extract the
values by looping throught that string,

I hope this helps?

Cor
Nov 20 '05 #2

P: n/a
Hi,

The showing of a string stops when there is a 00 byte in it.
However, normaly it is there in its full lenght and you can extract the
values by looping throught that string,

I hope this helps?

Cor
Nov 20 '05 #3

P: n/a
Patrick,
In addition to Cor's comments relating to the display of strings in .NET
with embedded NULL chars.

I'm not sure if the following is your problem or not, as the following in in
relation to calling a Win32 API, not a COM object per se (VB6 would be
calling a COM object).

See Adam Nathan's book ".NET and COM - The Complete Interoperability Guide"
by SAMS press for details.

Basically because of the way the Interop Marshaler works strings marshaled
from unmanaged code to managed code are terminated at the NULL char.

To "return" your null embedded string (from an API) you can use a
System.IntPtr.

Dim ptr As System.IntPtr
ptr = Marshal.AllocHGlobal(1024)

' call your API

Dim s As String
s = Marshal.PtrToSTringAnsi(ptr, length)

Marshal.FreeHGlobal(ptr)

Where the 1024 is the maximum size of string that can be returned & length
is the actual length of the string returned, its best if length was returned
by the API.

Can you change the VB6 DLL to be an Array of Bytes or Shorts (16 bit
integers) instead of a String?

Note Adam's book covers the interop marshaler in great detail, another
section may have insite into your problem.

Hope this helps
Jay

"DotNetJunkies User" <User@-NOSPAM-DotNetJunkies.com> wrote in message
news:O$*************@tk2msftngp13.phx.gbl...
I am calling a VB6 dll from a vb.net windows application that returns an array of strings. My issue is it seems to truncate after a NULL character.
For Example VB 6 is returning a string with the HEX value of 4E 31 00 00 01
00 20 20 20 20 20 00 00 00 20 20 20 31 32 30. But when it gets back to
Vb.net all I have is 4E 31 or N1. Now I can return the same string as a
return value of a function and I get it all.
Please Help,

Patrick Horn

---
Posted using Wimdows.net NntpNews Component -

Post Made from http://www.DotNetJunkies.com/newsgroups Our newsgroup

engine supports Post Alerts, Ratings, and Searching.
Nov 20 '05 #4

P: n/a
Patrick,
In addition to Cor's comments relating to the display of strings in .NET
with embedded NULL chars.

I'm not sure if the following is your problem or not, as the following in in
relation to calling a Win32 API, not a COM object per se (VB6 would be
calling a COM object).

See Adam Nathan's book ".NET and COM - The Complete Interoperability Guide"
by SAMS press for details.

Basically because of the way the Interop Marshaler works strings marshaled
from unmanaged code to managed code are terminated at the NULL char.

To "return" your null embedded string (from an API) you can use a
System.IntPtr.

Dim ptr As System.IntPtr
ptr = Marshal.AllocHGlobal(1024)

' call your API

Dim s As String
s = Marshal.PtrToSTringAnsi(ptr, length)

Marshal.FreeHGlobal(ptr)

Where the 1024 is the maximum size of string that can be returned & length
is the actual length of the string returned, its best if length was returned
by the API.

Can you change the VB6 DLL to be an Array of Bytes or Shorts (16 bit
integers) instead of a String?

Note Adam's book covers the interop marshaler in great detail, another
section may have insite into your problem.

Hope this helps
Jay

"DotNetJunkies User" <User@-NOSPAM-DotNetJunkies.com> wrote in message
news:O$*************@tk2msftngp13.phx.gbl...
I am calling a VB6 dll from a vb.net windows application that returns an array of strings. My issue is it seems to truncate after a NULL character.
For Example VB 6 is returning a string with the HEX value of 4E 31 00 00 01
00 20 20 20 20 20 00 00 00 20 20 20 31 32 30. But when it gets back to
Vb.net all I have is 4E 31 or N1. Now I can return the same string as a
return value of a function and I get it all.
Please Help,

Patrick Horn

---
Posted using Wimdows.net NntpNews Component -

Post Made from http://www.DotNetJunkies.com/newsgroups Our newsgroup

engine supports Post Alerts, Ratings, and Searching.
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.