System.Runtime.InteropServices (Urgent)

  • Thread starter Thread starter Ahmet
  • Start date Start date
A

Ahmet

Hi All;

I have one dll in which I have some function calls about a legend system
that I dont want to rewrite using c#

In this DLL I have function prototype like below;

#define SWCOMM_API extern "C" __declspec(dllexport)
SWCOMM_API int SwCmdSend(int Code, char *Txn, char *TxnSub, char *src, int
time);

Using interop. services, I make on dll call to this function using the code
below

[DllImport("SwComm.dll")]
public static extern int SwCmdSend(int pi_code, string Txn, string TxnSub,
string src, int time);

In c#, my return value is -256 (that I am sure SwCmdSend func in dll never
returns this value)
and operation fails.
When I use same dll with same parameters in one vc++ MFC program return
value is as expected and
operation is completed.
What is wrong in this example with dll usage in c# ?
Thanks..
 
Ahmet,

Does the SwCmdSend function modify the contents of the Txn, TxnSub or
src parameters? If they do not, then you should use this definition:

[DllImport("SwComm.dll"), CharSet=CharSet.Ansi)]
public static extern in SwCmdSend(
int pi_code,
[MarshalAs(UnmanagedType.LPStr)]
string Txn,
[MarshalAs(UnmanagedType.LPStr)]
string TxnSub,
[MarshalAs(UnmanagedType.LPStr)]
string src,
int time);

If you are modifying the contents of the string, then you need to change
the string parameter to a StringBuilder type, and then make sure you
pre-allocate the buffer (by setting the Capacity property) before you pass
it to the method to be called.

Hope this helps.
 
I change property and will try second one.
Thanks..


Nicholas Paldino said:
Ahmet,

Does the SwCmdSend function modify the contents of the Txn, TxnSub or
src parameters? If they do not, then you should use this definition:

[DllImport("SwComm.dll"), CharSet=CharSet.Ansi)]
public static extern in SwCmdSend(
int pi_code,
[MarshalAs(UnmanagedType.LPStr)]
string Txn,
[MarshalAs(UnmanagedType.LPStr)]
string TxnSub,
[MarshalAs(UnmanagedType.LPStr)]
string src,
int time);

If you are modifying the contents of the string, then you need to change
the string parameter to a StringBuilder type, and then make sure you
pre-allocate the buffer (by setting the Capacity property) before you pass
it to the method to be called.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ahmet said:
Hi All;

I have one dll in which I have some function calls about a legend system
that I dont want to rewrite using c#

In this DLL I have function prototype like below;

#define SWCOMM_API extern "C" __declspec(dllexport)
SWCOMM_API int SwCmdSend(int Code, char *Txn, char *TxnSub, char *src, int
time);

Using interop. services, I make on dll call to this function using the code
below

[DllImport("SwComm.dll")]
public static extern int SwCmdSend(int pi_code, string Txn, string TxnSub,
string src, int time);

In c#, my return value is -256 (that I am sure SwCmdSend func in dll never
returns this value)
and operation fails.
When I use same dll with same parameters in one vc++ MFC program return
value is as expected and
operation is completed.
What is wrong in this example with dll usage in c# ?
Thanks..
 
Back
Top