2

I'm trying to create a mixed mode dll with a few function exported for calling managed code from say, Delphi and C++.

I got it working but now I'm only getting System.StackoverflowException in the debugger and I'm wondering what I'm doing wrong.

The code is quite simple

unmanaged.h

#pragma once
#pragma unmanaged

#include <Windows.h>

typedef void (*ByteCallback)(unsigned char * bytes, int len);

namespace Something { 

extern "C"{

__declspec(dllexport) void InteropInit(ByteCallback responseCallback, ByteCallback requestInformationCallback);
}

};

unmanaged.cpp

#include "Stdafx.h"
#include "Managed.h"
#include "Unmanaged.h"

#pragma unmanaged

namespace Something{

void InteropInit(ByteCallback responseCallback, ByteCallback requestInformationCallback) {
 Something::ManagedInit(); 
}

};

managed.h

#include "Stdafx.h"
#pragma managed

namespace Something{

void ManagedInit();

};

managed.cpp

#include "Stdafx.h"
#include "Managed.h"
#include <string>

#pragma managed

namespace Something {
void ManagedInit() {       
    System::Console::WriteLine("Hallo");
};
};

Stacktrace (this goes on and on and on until visual studio wont show any more lines)

ntdll.dll!_RtlpCallVectoredHandlers@12()  + 0x2f244 bytes   
ntdll.dll!_RtlCallVectoredExceptionHandlers@8()  + 0x12 bytes   
ntdll.dll!_RtlDispatchException@8()  + 0x19 bytes   
ntdll.dll!_KiUserExceptionDispatcher@8()  + 0xf bytes   
ntdll.dll!_RtlpCallVectoredHandlers@12()  + 0x2f244 bytes   
ntdll.dll!_RtlCallVectoredExceptionHandlers@8()  + 0x12 bytes   
ntdll.dll!_RtlDispatchException@8()  + 0x19 bytes   
ntdll.dll!_KiUserExceptionDispatcher@8()  + 0xf bytes   

Could someone please help me?

JMax
  • 26,109
  • 12
  • 69
  • 88
Thomas
  • 45
  • 6
  • It looks like there is some exception being thrown somewhere. Have you tried debugging and finding out what is it? – svick Nov 18 '11 at 23:28
  • Does your real code not use `responseCallback` and `requestInformationCallback`, or did you oversimplify your question? – ildjarn Nov 18 '11 at 23:45
  • It's in the debugger i get the stackoverflowexception and the stacktrace you see here. – Thomas Nov 19 '11 at 10:26
  • My real code uses responseCallback and requestInformationCallback. I've removed all the code that uses them and the rest of the logic and I still get this exception. I've tried to create a new project and use the same code, thinking i've screwed up my project settings, but with no luck. – Thomas Nov 19 '11 at 10:29
  • I used AddVectoredExceptionHandler to add an exception handler, and the exceptioncode I get from that was 3221225725 (C00000FD) – Thomas Nov 19 '11 at 12:43
  • It seems C00000FD is an exception code for stack overflow, so that doesn't help much, I think. – svick Nov 19 '11 at 14:01

1 Answers1

2

I don't know what exatly you're doing, but this

typedef void (*ByteCallback)(unsigned char * bytes, int len);

should be a windows callback function, right? Almost all callbacks use stdcall, so

typedef void (__stdcall *ByteCallback)(unsigned char * bytes, int len);

might make it work. Of course, you need to add __stdcall to the function you're passing too.

Evan Dark
  • 1,311
  • 7
  • 7
  • Of course... I feel a litte bit stupid now. I used stdcall where I was calling Init from. This solved the issue. – Thomas Nov 19 '11 at 21:38