473,394 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2328
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is...
5
by: vertigo | last post by:
Hello I use some win 32 API function for example: HANDLE CreateFile( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD...
4
by: Sačo Zagoranski | last post by:
Hi, I'm trying to play an AVI file from memory with MCI. The documentation is great if you want to play a file from a file but directly from memory... almost nothing. After hours of...
6
by: Pucca | last post by:
I have a program that originally compiles into a exe file. I changed the compile option to generate dll file. This program calls a com component. Can I use pinvoke in C# to call it? The...
8
by: Rajesh Soni | last post by:
Hi! I'm getting a PInvoke error while trying to execute the following code... declaration: Structure POINTAPI Dim x As IntPtr
0
by: sd | last post by:
Hi all, I have seen this useful Add-in to get the proper Dllimport sentence to use native code in managed applications. I have tested that add-in in VS.NET 2005 (with .NET CF 2.0) and I have...
7
by: Rymfax | last post by:
I would really appreciate it if someone could help me figure out what I'm doing wrong trying to PInvoke SetupDiEnumDriverInfo. All the other PInvokes i've done up to this point work fine. Whenver...
0
by: Benosham | last post by:
I have been playing around with trying to PInvoke GDI+ from C#, I made recently made the transition from C/C++ to C# and I really like the language, however being the old fashioned programmer I am I...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
3
by: Siegfried Heintze | last post by:
Can someone kindly help me finish my attempt at pinvoke? HKL is a void* and I don't know how to translate it. //static extern UInt32 MapVirtualKeyExW(UInt32 uCode, UInt32 uMapType, HKL dwhkl); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.