DllImport Syntax

A

Arsen V.

Hi,

I have to use a functio that is inside of a DLL.

Using DllImport directive, I am able to point to the proper "public static
extern void".

However, the function takes a "char*" as one of its inputs. The DLL changes
the value of that char*. When used from inside VB, it works great. The
function declaration of ByVal buf As String works okay provided that I first
initialize the buf to be a string of 1000 spaces.

In C#, the DLL function is not changing the value of the buf string - it
remains empty 1000 spaces.

Thanks,
Arsen
 
M

Michael C

Arsen V. said:
Hi,

I have to use a functio that is inside of a DLL.

Using DllImport directive, I am able to point to the proper "public
static
extern void".

However, the function takes a "char*" as one of its inputs. The DLL
changes
the value of that char*. When used from inside VB, it works great. The
function declaration of ByVal buf As String works okay provided that I
first
initialize the buf to be a string of 1000 spaces.

In C#, the DLL function is not changing the value of the buf string - it
remains empty 1000 spaces.

Use a string builder. Although I'm sure I've got it to work before without a
string builder but can't remember how. See code below.

Michael
[DllImport("kernel32", EntryPoint= "GetComputerNameA", CharSet =
CharSet.Ansi)]

public static extern int GetComputerName(System.Text.StringBuilder lpBuffer,
ref int nSize);

[STAThread]

static void Main()

{

System.Text.StringBuilder x = new System.Text.StringBuilder(new string(' ',
500));

int len = x.Length;

GetComputerName(x, ref len);

}


Michael
 
T

TerryFei

Hi Arsen,

Thanks for Thi and Michael's reply. I just wanted to check how things are
going and whether or not your problem has been resolved.

If there is any question, please feel free to join the community and we are
here to support you at your convenience. Thanks again and have a nice day.

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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