DllImport Syntax

  • Thread starter Thread starter Arsen V.
  • Start date Start date
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
 
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
 
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.)
 
Back
Top