I have written a C DLL and some C# code to test including this DLL and executing functions from it. I am not too familiar with this process, and am receiving a PInvokeStackImbalance exception whenever my DLL function is called from the C# source code. The code is as follows (I have commented most code out to isolate this problem):
C# Inclusion code:
using System;
using System.Runtime.InteropServices;
using System.IO;
namespace TestConsoleGrids
{
class Program
{
[DllImport("LibNonthreaded.dll", EntryPoint = "process")]
public unsafe static extern void process( long high, long low);
static void Main(string[] args)
{
System.Console.WriteLine("Starting program for 3x3 grid");
process( (long)0, (long)0 );
System.Console.ReadKey();
}
}
}
C++ DLL Function Code
extern "C" __declspec(dllexport) void process( long high, long low );
void process( long high, long low )
{
// All code commented out
}
Visual Studio generated dllmain code (I do not understand this construct, so I am including it)
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
The details of the exception are:
A call to PInvoke function 'TestConsoleGrids!TestConsoleGrids.Program::process' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.