Hi,
Yes you are right of course. I made a simple application to demonstrate my
problem. It is containing only the nececary. I also made source for download
if that is more easy on
http://www.mestdagh.biz/kieken/CrashTest.zip The dll
I made for the demo is just calling the callback with the same data. this is
the complete project. Using the garbage collector I can let it reproduce the
error 1 on 2 times.
using System;
using System.Windows. Forms;
using System.Runtime. InteropServices ;
namespace DllCrashDemo
{
public partial class Form1 : Form
{
private Api api;
public Form1()
{
InitializeCompo nent();
api = new Api(this);
api.OnShow += api_OnShow;
}
void api_OnShow(obje ct sender, Api.ShowItArgs e)
{
listBox.Items.A dd(e.text);
}
private void crashButton_Cli ck(object sender, EventArgs e)
{
api.show("This is a crashtest");
}
}
public class Api
{
private Control owner;
public Api(Control control)
{
owner = control;
ApiDll.SetShow( cbShow);
}
public void show(string text)
{
ApiDll.ShowIt(t ext);
GC.Collect();
}
public class ShowItArgs : EventArgs
{
public string text;
}
public delegate void Show(object sender, ShowItArgs e);
public event Show OnShow;
private void cbShow(string text)
{
if (OnShow == null)
return;
ShowItArgs e = new ShowItArgs();
e.text = text;
owner.Invoke(On Show, new object[] { this, e });
}
}
public static class ApiDll
{
private const string crashDllName = @"C:\Program
Files\Borland\D elphi7\Projects \CrashDll.dll";
public delegate void OnShow([MarshalAs(Unman agedType.LPStr)]string
txt);
[DllImport(crash DllName)]
public static extern void
ShowIt([MarshalAs(Unman agedType.LPStr)]string txt);
[DllImport(crash DllName)]
public static extern void SetShow(OnShow cbShow);
}
}
--
rgds, Wilfried
http://www.mestdagh.biz
"Willy Denoyette [MVP]" wrote:
>
"Wilfried Mestdagh" <Wi************ **@discussions. microsoft.comwr ote in
message news:87******** *************** ***********@mic rosoft.com...
| Hi Nicholas,
|
| You have an API function SetLoggedOn, then it is storing in that dll
the
| address of the callback function. When someone logs on, it is calling
the
| callback function.
|
| SetLoggedOn installs the callback pointer into the DLL. The DLL calls back
| to the pointer when the application is logged on to the server.
|
| You have to tell it to stop calling that callback, since your
instance
| goes out of scope.
|
| But the DLL needs the callback and other callback for many other events.
The
| DLL comunicate with a server and data can arrive.
|
| What I think you should do is make this event static, and then make
your
| delegate to a static method.
|
| I think I tryed already to make the procedure static with same result. But
I
| tryed in the last hours so many things I'm not sure anymore (including
| banging my head on keyboard and monitore)...
|
| --
| rgds, Wilfried
| http://www.mestdagh.biz
We have to see how and where you create an instance of your delegate, so,
please post a short but complete repro that illustrates your issue.
incomplete code snips like you posted aren't of great help.
Willy.