473,386 Members | 1,654 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,386 software developers and data experts.

vb.net strings passing Win32 Dlls - HELP

I'm not sure what other groups this should go to, but I'm trying to
write a Win32 DLL that I can call from my vb.net app. I'm having a
real tough time, as this is printing a different char set than what I
expect. I'm not sure if I'm doing something wrong or its not setup
right, but it looks like Chinese characters coming back. Here is all
the code:

Win32 exported function:

int CDllTest::TestFunc3(TCHAR* lStr, int* lLen)
{
TCHAR* x=NULL;

x = (char *)calloc(10000,sizeof(char));
strcpy(x, "abcabcd");
strcpy(lStr, x);

*lLen = strlen(lStr);
return 0;
}

VB.net method:

Declare Auto Function TestFunc3 Lib "TestDLL" (ByVal msg As
StringBuilder, ByRef msgLength As IntPtr) As Integer

Dim s1 As StringBuilder
Dim i2 As IntPtr

s1 = New StringBuilder(1000)
i1 = TestFunc3(s1, i2)
Trace.WriteLine(s1.ToString & " " & i2.ToInt32)

When trace writes s1.tostring, it seems like chinese characters,
however the length is correct, so its got something to do with the way
its marshalled I guess.

Any help would be great !!!!!

Mark

Jan 11 '06 #1
4 3665
> x = (char *)calloc(10000,sizeof(char));

You never free the memory you allocate here so the function is
leaking.

strcpy(x, "abcabcd");
strcpy(lStr, x);
Why not just strcpy(lStr, "abcabcd") directly?

You may also want to pass in the size of the lStr buffer to make sure
you don't overrun it.

Declare Auto Function TestFunc3 Lib "TestDLL" (ByVal msg As
StringBuilder, ByRef msgLength As IntPtr) As Integer


Since you used strcpy above I assume you're using an ANSI build of the
DLL where TCHAR becomes char. In that case you shouldn't use the Auto
modifier on your Declare statement because that will cause the string
to be treated as Unicode on recent versions of Windows.

And the msgLength parameter should be of type Integer, not IntPtr.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 11 '06 #2
Thanks Mattias !!!!!!

It was the Auto causing it.

Jan 11 '06 #3
One other thing mattias,

Is it possible to reinitialize a StringBuilder object, or is the
correct way just to keep reallocating new ones ? I want to just create
1 instance of a stringbuilder, then just use it over and over again ?

Jan 11 '06 #4
>or is the correct way just to keep reallocating new ones ?

There's nothing wrong with it (unless this happens to be a very
performance critical part of your code called in a tight loop and ou
need to reduce the numbe of objects created).
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 12 '06 #5

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

Similar topics

2
by: Jorgen Grahn | last post by:
I couldn't think of a good solution, and it's hard to Google for... I write python command-line programs under Win2k, and I use the bash shell from Cygwin. I cannot use Cygwin's python package...
6
by: Laszlo Zsolt Nagy | last post by:
Sorry, I realized that the import zlib was not executed from my (working) service. So here is the question: why can't I use zlib from a win32 service? Is there any way to make it working? ...
2
by: Claire | last post by:
After giving up on passing nested structs to an unmanaged DLL, I planned that I'd pass a simple buffer of bytes (the same size as the struct) to the dll, convert to a memory stream and read in the...
2
by: ramialhasan | last post by:
I have good experience with COM ATL object, but recently I needed to deal with Win32 DLLs. I have some question relating to their model of work. In COM objects every client of the com server...
6
by: _R | last post by:
I've had to write a lot of code to interface C# to older Win32 DLLs. Basically, an unmanaged C++ class talks directly to the Win32 DLL. A managed C++ class encloses the unmanaged C++ class. C#...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
2
by: Tom | last post by:
I have a main VB.NET program which instantiates a number of other VB.NET DLLs. This DLLs all use routines from a 'base' DLL (which contains a number of common routines); so currently I...
24
by: Dave | last post by:
I understand that VS.NET is supposed to compile native Win32 apps that do not require the .Net runtime. If that's the case then there is something else from the VS200x package that is required. ...
12
by: scottt | last post by:
hi, I am having a little problem passing in reference of my calling class (in my ..exe)into a DLL. Both programs are C# and what I am trying to do is pass a reference to my one class into a DLL...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.