Intermittent Access Violation when using C# to access C++ DLL -


i'm linking c# quite complex c++ dll. need create lot of dllexport funtions can use dll in c#. started, added .cpp file , created simple test function:

c++:

extern "c" __declspec(dllexport) int32_t test(){     return 10; } 

c#:

[stathread] static void main() {     console.writeline(test()); }  [dllimport("test.dll", entrypoint = "test", callingconvention = callingconvention.cdecl,exactspelling = true)] public static extern int32 test(); 

this test works 90% of time , suddenly....

the program '[4712] test.vshost.exe' has exited code -1073741819 (0xc0000005) 'access violation'. 

everything seems fine, no idea causes or how begin track down problem. it's weird intermittent. i'm not c++ programmer i've no idea might cause behaviour, or how debug , find problem.

hope kind soul can help.

this

setlasterror = true 

is useless unless using windows api.

and set

callingconvention = callingconvention.cdecl 

like

[dllimport("test.dll", entrypoint = "test", callingconvention = callingconvention.cdecl, exactspelling = true)] 

note there code other method executed. dll can (and does) have dllmain method. in method "global" variables of dll initialized, , methods must called single time. method called @ least twice: dll_process_attach when dll loaded , dll_process_detach when process ends/the dll unloaded.


Comments