So I'm getting this exception when trying to import functions from a dll I made. This is what the error message says (while running in debug mode) :
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
This is really bothering me because I somewhat need this to work. So I'll post some code showing the weird thing. And yes, this is from the GDK, I have some use for what I'm doing, and no, I'm not trying to compete with DGDK.NET. But heres the code I'm using to export the functions from the C++ *.Dll ( MSVC++ 2008 express ) :
#define EXPORT __declspec(dllexport)
extern "C"
{
EXPORT int MouseClick_db(); // return dbMouseClick() is the actual code.
}
Now, that seems fine, judging off the articles I've read about importing unmanaged code into .NET.
Now, here is the .NET code I've been using to import this function:
public static int MouseClick()
{
return MouseClick_db();
}
[DllImport(@"ManagedGDK")]
static extern int MouseClick_db();
Now, in the main loop ( I don't have a window created yet), I basically just loop C#. I have it so that the DarkGDK function is defined within the dll from c++. What I did for that is I made 3 functions : Initialize_db(), Loop_db(), and Unload_db(). These are called in sequential order in the DarkGDK() function declaration. I have it so you set these from C#. For that, I made a delegate called FunctionPointerDelegate(); Heres the code I used for that:
public delegate void FunctionPointerDelegate();
public static FunctionPointerDelegate Update;
public static FunctionPointerDelegate Initialize;
public static FunctionPointerDelegate Unload;
[DllImport(@"ManagedGDK")]
static extern void SetInitFunction(FunctionPointerDelegate function);
[DllImport(@"ManagedGDK")]
static extern void SetUpdateFunction(FunctionPointerDelegate function);
[DllImport(@"ManagedGDK")]
static extern void SetUnloadFunction(FunctionPointerDelegate function);
And here is the code that assigns the functions:
DarkGDK.DarkGDK.Initialize = myGame1.Initialize;
DarkGDK.DarkGDK.Update = myGame1.Update;
DarkGDK.DarkGDK.Unload = myGame1.Unload;
And the functions from myGame1.Initialize/Update/Unload:
public void Initialize()
{
DarkGDK.DarkGDK.SyncOn();
DarkGDK.DarkGDK.Print("Hello world! This is C#!!!");
}
public void Update()
{
DarkGDK.DarkGDK.WaitKey();
while (true)
{
DarkGDK.DarkGDK.Sync();
}
}
public void Unload()
{
}
Heres the DarkGDK function declaration from the c++ dll:
void DarkGDK()
{
initFunction();
while ( LoopGDK() )
{
updateFunction();
}
unloadFunction();
}
Now, will I have to run the DarkGDK function from C#? If so, I have tried that also, but I got the AccessViolationException with that as well. Heres the code I used for that:
void Run_db()
{
DarkGDK();
}
It is exported like the other functions, extern "C" and __declspec(dllexport).
So, after my very unordered post, do any of you think you could help? Thanks in advance
WC Physics - PhysX wrapper for Dark GDK.