472,784 Members | 810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 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 2285
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.