Dllimport

P

paul f

Hi ,
Can somebody let me know how I would implement a dllimport and call
function "Paul" from a C++ dll?

calling syntex:
int __cdecl Paul(char *,char *,unsigned int &,unsigned char *,unsigned
int

);
 
P

paul f

Can somebody let me know how I would implement a dllimport and call
function "Paul" from a C++ dll?
calling syntex:
int __cdecl Paul(char *,char *,unsigned int &,unsigned char *,unsigned int

Does this work?

using System.Runtime.InteropServices;

[DllImport("__cdecl.dll", EntryPoint="Paul")]
static extern int Paul(string arg1, string arg2, System.UInt32 arg3, byte
arg4, System.UInt32 arg5);

No this did not work, the dll file is declared as follows in C++ code:
_declspec (dllexport) int Paul(char *inputData1, char *OutputData,
unsigned int iLenData, unsigned char *inputData2, unsigned int iLen)
`

here is my code so far, getting error Unable to find entry point named
"function" in DLL "Paul.dll":

public partial class Form1 : Form
{
[DllImport("C:\\PAul.dll",EntryPoint ="Function")]

static extern int Function(string Inout1, string Output,
System.UInt32 iLenData, string Input2, System.UInt32 iLen);

public Form1()
{
string Input1= "rrdfgfdgdfg554654gfdg";
string Input2= "dfsfsd45435grg";
string Output="";
int ret;
ret = Function(Input1, Output,25,Input2,32);
InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)
{


}
}
 
B

Bjørn Brox

paul f skrev:
Can somebody let me know how I would implement a dllimport and call
function "Paul" from a C++ dll?
calling syntex:
int __cdecl Paul(char *,char *,unsigned int &,unsigned char *,unsigned int
Does this work?

using System.Runtime.InteropServices;

[DllImport("__cdecl.dll", EntryPoint="Paul")]
static extern int Paul(string arg1, string arg2, System.UInt32 arg3, byte
arg4, System.UInt32 arg5);

No this did not work, the dll file is declared as follows in C++ code:
_declspec (dllexport) int Paul(char *inputData1, char *OutputData,
unsigned int iLenData, unsigned char *inputData2, unsigned int iLen)
`

here is my code so far, getting error Unable to find entry point named
"function" in DLL "Paul.dll":

public partial class Form1 : Form
{
[DllImport("C:\\PAul.dll",EntryPoint ="Function")]

static extern int Function(string Inout1, string Output,
System.UInt32 iLenData, string Input2, System.UInt32 iLen);

public Form1()
{
string Input1= "rrdfgfdgdfg554654gfdg";
string Input2= "dfsfsd45435grg";
string Output="";
int ret;
ret = Function(Input1, Output,25,Input2,32);
InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)
{


}
}

Just remember: a string is NOT the same as char *
 
P

paul f

Assuming your first statement was correct, I'm not surprised...

1) What is the name of the DLL?

2) What is the name of the function that you are trying to call?

Use the table in the web page below to map unmanaged language types to .NET
class names:http://msdn.microsoft.com/en-us/library/ac7ay120.aspx

Hi Mark,
I fixed this issue by adding extern "C" to header file and cpp file
for that particilar function.
Also looking at this table for char* i need to use string (which is
correct).

but I have one more issue I am getting error when the function
attempts to write string data to output string:
Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.
 
P

Pavel Minaev

Also looking at this table for char* i need to use string (which is
correct).

Not really. If your function writes into the buffer that char* points
to, then you should not use a String in P/Invoke declaration; you
should use a StringBuilder, and set its capacity to the expected size
of data in C# code. And if your C++ function does not write into the
buffer, then you should declare those arguments as const char* and not
char*
 

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