Click to See Complete Forum and Search --> : passing strings


pawan_marwar
March 27th, 2004, 10:42 AM
I want to pass a VB.NET string variable to my VC dll. The function in dll uses LPSTR. why is it that i have to set the string to some initial value and then pass this string. for example -

Dim str as String
str = "11111111111"

if i don't do this, i get an error of Nullreference ptr. this style is quite an improper one. if the new value put by the DLL is greater than the size specified, i.e., "11111111111", then that value is truncated to the VB String size. if the new value filled in by DLL is less than initial size of "11111111111", then that much goes as a waste.

is there any other method to do this stuff. i've tried interop marshal concept using Intptr. that too fails. Intptr works fine when i've a string inside a structure. but if that string goes individually, IntPtr fails. i don't understand why it fails here.

help

pawan

pawan_marwar
March 27th, 2004, 10:49 AM
what is the difference between a char() array and String in VB.NET

when i use char(), the value obtained is NULL.

Craig Gemmill
March 27th, 2004, 01:14 PM
Try using a StringBuilder instead.

DeepButi
March 29th, 2004, 06:44 AM
Intptr works fine when i've a string inside a structure. but if that string goes individually, IntPtr fails.
When you define str inside a structure, memory is already allocated to it in a fixed length basis. When you define str "alone", it has no length until you allocate space to it. When your C code tries to use the string, there is nothing to be used ... so if you use a string, you need to initialize it to "111..." or whatever you like to be sure there is memory allocated to hold whatever you need.
Your C code does not (can not!) allocate or deallocate space since the variable is defined beyond its scope, so the calling process must do it.

Hope it helps