473,545 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Callbacks from unmanaged code...

Hi there,

I'm new to C# (but not to C++), and I'm trying to create a web service that
calls some of our older software, located in a DLL. I'm getting most the
calls to work ok, but one of them needs a callback into the managed code. I
thought I had it all worked out, but it seems like when the callback is
getting hit, things might be going out of scope and crashing the app.

Here's my little web service function; I hacked out a bunch of other stuff
for brevity, but the general idea is, I call the "Initialize " function,
providing a callback to the DLL so the DLL can call me when certain
operations in the "LoginUser2 " complete.

You can see where I tried to keep the callback around in a session object,
but, being a novice, I'm not sure this is the right approach. Essentially, I
need this callback alive for the whole life of the user's session.

[WebMethod(Enabl eSession = true)]
public string HelloArguments( string sLogin, string sPassword)
{
Test.TNInitiali zeCallbackType cb = new
Test.TNInitiali zeCallbackType( Test.TNAPICallb ack);
Session["Callback"] = cb;

hWho = Test.Initialize (cb);

result2 = Test.LogonUser2 (hWho, sLogin, sPassword);

return sResult;
}

and some defs:

class Test
{
[DllImport("mydl l.dll")]
public static extern Int32 LogonUser2(IntP tr hWho, string pszLogin,
string pszPassword);

public delegate IntPtr TNInitializeCal lbackType(IntPt r hWho, Int32
a1, Int32 a2, Int32 a3);

[DllImport("mydl l.dll")]
public static extern IntPtr Initialize(TNIn itializeCallbac kType
callback);

public static IntPtr TNAPICallback(I ntPtr hWho, Int32 a1, Int32 a2,
Int32 a3)
{
return (IntPtr)0;
}

Thanks for any help or pointers as to where I can read up on this.

Apr 11 '06 #1
2 2100
In .NET, the calling convention for methods is StdCall. What is the calling
convention for your unmanaged *.dll? I'm assumming it is probably cdecl,
which will cause problems in your managed callback. The stack is probably
being popped one too many times after your call back executes.

You can use the CallingConventi on property of the DllImport attribute to
define the calling convention for that method, but I'm not sure if this can
be applied to delegates.

[DllImport("mydl l.dll", CallingConventi on=CallingConve ntion.Cdecl )]
public static extern IntPtr Initialize(TNIn itializeCallbac kType callback);

Not sure if the above code will work for a delegate, but you can give it a
shot. If you have the unmanaged souce code, you can probably create another
method called something like "InitializeStdC all" using the StdCall calling
convention and then in there, call "Initialize ". This will act as a wrapper.

Hope this helps or points you in the right direction.

"cada0310" wrote:
Hi there,

I'm new to C# (but not to C++), and I'm trying to create a web service that
calls some of our older software, located in a DLL. I'm getting most the
calls to work ok, but one of them needs a callback into the managed code. I
thought I had it all worked out, but it seems like when the callback is
getting hit, things might be going out of scope and crashing the app.

Here's my little web service function; I hacked out a bunch of other stuff
for brevity, but the general idea is, I call the "Initialize " function,
providing a callback to the DLL so the DLL can call me when certain
operations in the "LoginUser2 " complete.

You can see where I tried to keep the callback around in a session object,
but, being a novice, I'm not sure this is the right approach. Essentially, I
need this callback alive for the whole life of the user's session.

[WebMethod(Enabl eSession = true)]
public string HelloArguments( string sLogin, string sPassword)
{
Test.TNInitiali zeCallbackType cb = new
Test.TNInitiali zeCallbackType( Test.TNAPICallb ack);
Session["Callback"] = cb;

hWho = Test.Initialize (cb);

result2 = Test.LogonUser2 (hWho, sLogin, sPassword);

return sResult;
}

and some defs:

class Test
{
[DllImport("mydl l.dll")]
public static extern Int32 LogonUser2(IntP tr hWho, string pszLogin,
string pszPassword);

public delegate IntPtr TNInitializeCal lbackType(IntPt r hWho, Int32
a1, Int32 a2, Int32 a3);

[DllImport("mydl l.dll")]
public static extern IntPtr Initialize(TNIn itializeCallbac kType
callback);

public static IntPtr TNAPICallback(I ntPtr hWho, Int32 a1, Int32 a2,
Int32 a3)
{
return (IntPtr)0;
}

Thanks for any help or pointers as to where I can read up on this.

Apr 12 '06 #2
Hi - thanks for your response!

The definition is actually __stdcall in the unmanaged code; here's the C++
prototype definition for the callback that has to be passed to C++:

typedef long (*TNAPI_CALLBAC K)(HANDLE, DWORD, DWORD, DWORD);
HANDLE CALLBACK TN_Initialize(T NAPI_CALLBACK pFunc);

So the idea is that in C# I need to call TN_Initilalize and pass it a
callback function, so that the unmanaged DLL can make calls to the provided
callback.

It seems to be (mostly) working; I can set breakpoints in the C# provided
callback, and it's getting called...but I think something's munged because
sometimes the app just croaks.
"rmacias" wrote:
In .NET, the calling convention for methods is StdCall. What is the calling
convention for your unmanaged *.dll? I'm assumming it is probably cdecl,
which will cause problems in your managed callback. The stack is probably
being popped one too many times after your call back executes.

You can use the CallingConventi on property of the DllImport attribute to
define the calling convention for that method, but I'm not sure if this can
be applied to delegates.

[DllImport("mydl l.dll", CallingConventi on=CallingConve ntion.Cdecl )]
public static extern IntPtr Initialize(TNIn itializeCallbac kType callback);

Not sure if the above code will work for a delegate, but you can give it a
shot. If you have the unmanaged souce code, you can probably create another
method called something like "InitializeStdC all" using the StdCall calling
convention and then in there, call "Initialize ". This will act as a wrapper.

Hope this helps or points you in the right direction.

"cada0310" wrote:
Hi there,

I'm new to C# (but not to C++), and I'm trying to create a web service that
calls some of our older software, located in a DLL. I'm getting most the
calls to work ok, but one of them needs a callback into the managed code. I
thought I had it all worked out, but it seems like when the callback is
getting hit, things might be going out of scope and crashing the app.

Here's my little web service function; I hacked out a bunch of other stuff
for brevity, but the general idea is, I call the "Initialize " function,
providing a callback to the DLL so the DLL can call me when certain
operations in the "LoginUser2 " complete.

You can see where I tried to keep the callback around in a session object,
but, being a novice, I'm not sure this is the right approach. Essentially, I
need this callback alive for the whole life of the user's session.

[WebMethod(Enabl eSession = true)]
public string HelloArguments( string sLogin, string sPassword)
{
Test.TNInitiali zeCallbackType cb = new
Test.TNInitiali zeCallbackType( Test.TNAPICallb ack);
Session["Callback"] = cb;

hWho = Test.Initialize (cb);

result2 = Test.LogonUser2 (hWho, sLogin, sPassword);

return sResult;
}

and some defs:

class Test
{
[DllImport("mydl l.dll")]
public static extern Int32 LogonUser2(IntP tr hWho, string pszLogin,
string pszPassword);

public delegate IntPtr TNInitializeCal lbackType(IntPt r hWho, Int32
a1, Int32 a2, Int32 a3);

[DllImport("mydl l.dll")]
public static extern IntPtr Initialize(TNIn itializeCallbac kType
callback);

public static IntPtr TNAPICallback(I ntPtr hWho, Int32 a1, Int32 a2,
Int32 a3)
{
return (IntPtr)0;
}

Thanks for any help or pointers as to where I can read up on this.

Apr 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1903
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion returns a pointer to a structure to its callback fucntion.The user should collect those structure fields in a buffer. In my managed code(i.e. in C# program), I've used a delegate for invoking callback function and I've declared the...
1
1662
by: vijaya | last post by:
I've to invoke a unmanaged dll fucntion in C# which uses a callback fucntion.The unmanaged dll fucntion returns a pointer to a structure to its callback fucntion.The user should collect those structure fields in a buffer. In my managed code(i.e. in C# program), I've used a delegate for invoking callback function and I've declared the...
23
2216
by: Brian | last post by:
I am very new to C# programming and have run into a problem. I apologize for the repost of this article. For some reason, my news reader attached it to an existing thread. First off, I have an SDK that I have written for C/C++ and would like to port it to C# if at all possible. Basically, I've got a structure that I need to pass to the...
4
4973
by: R. MacDonald | last post by:
Hello, all, I have a .NET application (VB) that passes the address of a delegate to unmanaged code in a DLL. The unmanaged code then uses the delegate as a call-back. This seems to work fine, but now I am worried about garbage collection. I am concerned that the location of the delegate might be altered as a result of other (unused)...
6
3251
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls to the function in the container. I allocate (and free in c) arrays to get garbage collection in both C and C#. After a few seconds I get an...
3
2192
by: kelvin.koogan | last post by:
I have an unmanaged DLL which calls my managed C++ app using a callback (standard C++ function pointer). I can receive the callbacks OK using a static function with global scope, but I can't find a way to get hold of a pointer to my Form to pass it the data. Any ideas? Application seems to have the only pointer to the form but no way for...
8
2265
by: DavidT | last post by:
Hello, at first, exuse if the following question is simple to solve, but i normaly coding with C# and now have to use C++/CLI for one project. My Problem is that i have to use a native c++ sdk to read pictures from ip cameras. I get the native c++ decoder worked so far and now try to send callbacks from unmanaged to managed code, to...
0
7487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7446
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.