471,348 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

help with pinvoke

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
Jul 21 '05 #1
7 2220
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.

Jul 21 '05 #2
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

"Eyal Safran" wrote:
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.

Jul 21 '05 #3


savage wrote:
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.

Jul 21 '05 #4
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" wrote:


savage wrote:
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.

Jul 21 '05 #5


savage wrote:
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.

Jul 21 '05 #6
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.

Jul 21 '05 #7
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.

"Eyal Safran" wrote:
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.

Jul 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Brett Robichaud | last post: by
5 posts views Thread by vertigo | last post: by
4 posts views Thread by Sao Zagoranski | last post: by
8 posts views Thread by Rajesh Soni | last post: by
3 posts views Thread by Siegfried Heintze | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.