Du maa godt se hvor langt jeg naaede.
C# kode:
using System;
namespace DemoNamespace
{
public class DemoClass
{
public static byte[] DemoMethod(byte[] a)
{
byte[] res = new byte[a.Length];
for(int i = 0; i < res.Length; i++) {
res[i] = (byte)(res[i] + 1);
}
return res;
}
}
}
C++ kode:
#include <iostream>
using namespace std;
unsigned char* demo_method(unsigned char *a, int len);
int main()
{
unsigned char a[] = { 1, 2, 3 };
unsigned char* a2 = demo_method(a, 3);
cout << a2[0] << " " << a2[1] << " " << a2[2] << endl;
return 0;
}
C++ glue kode:
#using <mscorlib.dll>
#using <demo.dll>
using namespace System;
using namespace System::Runtime::InteropServices;
unsigned char* demo_method(unsigned char *a, int len)
{
array<Byte>^ a2 = gcnew array<Byte>(len);
Marshal::Copy((IntPtr)a, a2, 0, len);
array<Byte>^ res2 = DemoNamespace::DemoClass::DemoMethod(a2);
unsigned char* res = new unsigned char[len];
Marshal::Copy(res2, 0, (IntPtr)res, len);
return res;
}
men det crasher!
:-(
Unhandled Exception: System.TypeInitializationException: The type initializer fo
r '<Module>' threw an exception. ---> System.AccessViolationException: Attempted
to read or write protected memory. This is often an indication that other memor
y is corrupt.
at _initterm_e((fnptr)* pfbegin, (fnptr)* pfend)
at <CrtImplementationDetails>.LanguageSupport.InitializeNative(LanguageSuppor
t* )
at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
at .cctor()
--- End of inner exception stack trace ---