help with pinvoke

G

Guest

hey all
i need to convert an unmanaged c++ dll and header to c#, ive done the header
structures but having problems with pinvoke signatures

method 1:
typedef int (_stdcall *pOmniConnect) (char * IpAddress,
unsigned int Port,
unsigned int Timeout,
unsigned char EncryptionKey[16],
HWND NotifyWindow);
IpAddress - ip as ascii string
Port - udp port as int
Timeout - in seconds 0-60
EncryptionKey - 16 byte binary of encryption key. formed from 2, 8 byte hex
inputs as a single 16 byte binary
NotifyWindow - window handle that gets reponses

ive converted that into
[DllImport("HAI_NetLib.dll", EntryPoint="_OmniConnect")]
private static extern int OmniConnect([MarshalAs(UnmanagedType.LPStr)]
string ipAddress, int port, int timeOut, [MarshalAs(UnmanagedType.LPStr)]
Byte encryptionKey, IntPtr hwnd);

when i test it i get
'the exception unknown software exception (0x0eedfade) occurred in the
application at location 0x77e649d3.'

ive tried changing UnmanagedType and even tried StringBuilder ipAddress, but
doesnt seem to work.

any ideas?

thanks
 
E

Eyal Safran

Hi,

typedef int (_stdcall *pOmniConnect) (char * IpAddress,
unsigned int Port,
unsigned int Timeout,
unsigned char
EncryptionKey[16],
HWND NotifyWindow);
is a function pointer which is used for callbacks from unmanaged to
managed.
which is a delegate in C#

and it will look like:
public delegate int OmniConnectEventHandler(string ipAddress,
uint
port,
uint
timeout,

byte[] encryptionKey,

IntPtr notifyWindow);

try:
[DllImport("HAI_NetLib.dll", EntryPoint="_OmniConnect")]
private static extern int OmniConnect(string ipAddress,
uint port,
uint timeOut,
string
encryptionKey,
IntPtr hwnd);

try adding CallingConvention=CallingConvention.StdCall in the DllImport
attribute.
or, if your method is part of a class, then the CallingConvention
should be ThisCall and you need to add as the first parameter to your
OmniConnect signature an "IntPtr instance", where the instance handle
will passed.

Can you show how you used the function pointer in your dll? not just
the function pointer definition, maybe I will have more information for
you.

Eyal.
 
G

Guest

im still lost, now my code looks like this

[DllImport("HAI_NetLib.dll", EntryPoint="_OmniConnect",
CallingConvention=CallingConvention.StdCall)]
private static extern int OmniConnect(string ipAddress, uint port, uint
timeOut, string encryptionKey, IntPtr hwnd);

public delegate int OmniConnectEventHandler(string ipAddress, uint port,
uint timeOut, byte[] encryptionKey, IntPtr hwnd);

[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

private void button1_click(...) {
IntPtr hWnd = FindWindow(null,"Form1"); // my test form
string key = "1010101101....etc";

OmniConnect("127.0.0.1", 4369, 30, key, hWnd);
}

i still get the same error, and how do i make use of that delegate?

thanks for your help, very much appriciated
 
E

Eyal Safran

savage said:
im still lost, now my code looks like this

I'm sorry for that...
is the cpp code yours? you have the sources?
where and how in the cpp code do you use the "pOmniConnect"?
I mean how is prototype of the OmniConnect function looks like? (not
the typedef).
I want to do some tests here, so I need to know a little more about
your cpp dll.
public delegate int OmniConnectEventHandler(string ipAddress, uint port,
uint timeOut, byte[] encryptionKey, IntPtr hwnd);
disregard the delegate for the meantime... maybe we'll use it.

Eyal.
 
G

Guest

the dll is not mine and i dont have the source, but i do have some samples of
using it in borland c++ and delphi along with the header file and pdf(same as
header file with abit more explanation).

ive requested info from the people who created it but its been ages and no
one got back to me so hopefully you can make sense out of it.

you can get them here:
http://tomuch.com/NetProtocolLib.zip

thanks man

Eyal Safran said:
im still lost, now my code looks like this

I'm sorry for that...
is the cpp code yours? you have the sources?
where and how in the cpp code do you use the "pOmniConnect"?
I mean how is prototype of the OmniConnect function looks like? (not
the typedef).
I want to do some tests here, so I need to know a little more about
your cpp dll.
public delegate int OmniConnectEventHandler(string ipAddress, uint port,
uint timeOut, byte[] encryptionKey, IntPtr hwnd);
disregard the delegate for the meantime... maybe we'll use it.

Eyal.
 
E

Eyal Safran

savage said:
the dll is not mine and i dont have the source, but i do have some samples of
using it in borland c++ and delphi along with the header file and pdf(same as
header file with abit more explanation).

ive requested info from the people who created it but its been ages and no
one got back to me so hopefully you can make sense out of it.

you can get them here:
http://tomuch.com/NetProtocolLib.zip

thanks man

Listen man, It's working...

[DllImport("HAI_NetLib.dll", EntryPoint="_OmniConnect",
CallingConvention=CallingConvention.StdCall)]
private static extern int OmniConnect(
string ipAddress,
uint port,
uint timeOut,
[MarshalAs(UnmanagedType.LPStr, SizeConst=16)]
string encryptionKey,
IntPtr hwnd);

you can do it with or without the "[MarshalAs(UnmanagedType.LPStr,
SizeConst=16)]"

private void button1_Click(object sender, System.EventArgs e)
{
IntPtr hWnd = FindWindow(null,"Form1"); // my test form
string key = "1010101101";
int ret = OmniConnect("192.168.0.2", 4096, 30, key, hWnd);
}

I even get a return value of 1 which is good...

Eyal.
 
E

Eyal Safran

I even looked with Ethereal and I saw my computer sending udp packets
to the destination IP & port, I don't know what it was supposed to
send, but it sent: 0x00, 0x01, 0x01, 0x00 (4 bytes of udp payload)

Eyal.
 
G

Guest

strange i rebooted the machine and it returned 1 without errors :)
now i'll be pulling my hair out trying to figure out how to recieve
wm_copydata.

well thank you Eyal for all your help.
 

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