Marshal, Interop, other way?

  • Thread starter Thread starter Krzysztof
  • Start date Start date
K

Krzysztof

I must use methods from standard dll im my C#code. In dll declaration of
method is like this:

typedef (__stdcall *ExecuteTaskFromXML)( const string& input, string&
output, DWORD& dwSize, LPVOID& pCtx );

How can I import (and use of course) this method? I try:
[DllImport("ExportDll.dll")]
public static extern void ExecuteTaskFromXML(String input, ref String
output, int dwSize, int pCtx);

but I've got error: System.NullReferenceException. (on String output)

What I must do? Some sample, docs or "entry point in codeproject" ;)) will
be best for me...

Please help

Regards
Krzysztof
 
C++ "string" and .NET's String are two very different beasts. While, in the
sense of P/Invoke, you can declare arguments as System.String for all
flavours of char*, TCHAR*, TSTR and so on, this trick won't work for the C++
"string" class because it is not a mere pointer to a sequence of characters
in memory.

You can do a trick by declaring a managed structure having the same layout
in memory as the C++ string class, but still it can cause many troubles
related to marshalling string data. If you have at least slim chance to
alter the unmanaged DLL function to return some API-compatible string type
like TCHAR, use it!

One more thing - the unmanaged function is declared as "__stdcall". I am not
sure this is the same that the "WINAPI" convention expected by the .NET's
P/Invoke by default.
 
Hi,
I must use methods from standard dll im my C#code. In dll declaration of
method is like this:

typedef (__stdcall *ExecuteTaskFromXML)( const string& input, string&
output, DWORD& dwSize, LPVOID& pCtx );

How can I import (and use of course) this method? I try:
[DllImport("ExportDll.dll")]
public static extern void ExecuteTaskFromXML(String input, ref String
output, int dwSize, int pCtx);

If you don't need an output do this

[DllImport(DLLNAME, EntryPoint=PROC_NAME,
CharSet=CharSet.Ansi, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool LoadExample(int anything,
String fileName);

If you need output you need to pass an IntPtr as reference to some
memory reserved by the marshal class.
 
One more thing - the unmanaged function is declared as "__stdcall". I am not
sure this is the same that the "WINAPI" convention expected by the .NET's
P/Invoke by default.

It is.



Mattias
 
If you need output you need to pass an IntPtr as reference to some
memory reserved by the marshal class.

Some sample?

How to do this...

Regards Krzysztof
 
Back
Top