[CONNECT C# / C++] Pass elementRef as parameter in call to MDL / C++ function from C#

I would like to pass a nativeElementRef from a .NET application to a library function written in MDL/C++. In V8i I declared the C++ function like this:

using SRI=System.Runtime.InteropServices;

namespace WhatEver
{
    class MyClass
    {
        [SRI.DllImport("mylib.dll", EntryPoint = "lib_MyFunction", CallingConvention = SRI.CallingConvention.StdCall)]
        static extern void lib_MyFunction(int elementRef);

and called the function like this:

lib_MyFunction(pElement.MdlElementRef());

Snippet from the MDL / CPP source file:

extern "C" lib_myFunction(ElementRef elmRefIn)
{
	MSElement   baseElm;

	elementRef_getElement(elmRefIn, &baseElm, elementRef_getElemSize(elmRefIn));

Now I would like to do the same in Connect.

I've declared the function like this:

[SRI.DllImport("mylib.dll", EntryPoint = "lib_MyFunction", CallingConvention = SRI.CallingConvention.StdCall)]
    static extern void lib_MyFunction(System.IntPtr elm);

and calls the function like this:

lib_myFunction(elm.GetNativeElementRef());

In MDL / CPP the function looks like this:

extern "C" void lib_myFunction(ElementRefP elmRef)
{
	MSElement		elm;

	elementRef_getElement(elmRef, &elm, elementRef_getElemSize(elmRef));

Both parts C# and C++ compiles fine but when the AddIn application is started, the element is identified and I call the C++ function MicroStation crashes. Please tell me why.

TIA.

Regards Evan