c++ - Getting AccessViolationException while Assigning Delegate from C# to FunctionPointer in NativeCode -


from c# code i'm trying call api *.c file. i'm getting accessviolationexception.

earlier getting badimageformatexception, solved putting both exe , dll in 1 location.

i suspect has callingconvention of cdecl or stdcall or platformtype=x86 or x64.

in c#. exe i've set cpu x86 , in nativecode.dll calling convention mentioned cdecl only.

i'm not able understand how solve issue,there many posts on web explaining problem somehow i'm not able find concrete solution issue.

in case of more details please let me know.

below mentioned code files can highlight problem:

header.h file

typedef int bool;  #ifndef false #define false               0 #endif  #ifndef true #define true                1 #endif  typedef bool regfunction( char *pregkey, char *pbuffer, int ibufsize ); 

implementation.c file

    #ifdef swscancode_exports     #define swscancode_api __declspec(dllexport)     #else     #define swscancode_api __declspec(dllimport)     #endif      #ifdef swscan_apis      swscancode_api bool swsetgetregkeyvalue  ( regfunction* pgetregkeyvalue );      #endif      swglobals*     pswglobals;       swscancode_api bool swsetgetregkeyvalue( regfunction * pgetregkeyvalue )     {         bool binitstatus;                // initialization status         binitstatus = true;              // assume initialization successful         pswglobals->pgetregkeyvalue = pgetregkeyvalue;        if(null!=pswglobals->pgetregkeyvalue)        {            printf("i don't know");        }        return( binitstatus );     } 

c# code

class program {     [unmanagedfunctionpointer(callingconvention.cdecl)]      public delegate int regfunction([marshalas(unmanagedtype.lpstr)]string pregkey,  [marshalas(unmanagedtype.lpstr)]string pbuffer, int ibufsize);      [dllimport("nativecode.dll", entrypoint = "swsetgetregkeyvalue", callingconvention = callingconvention.cdecl)]     [return:marshalasattribute(unmanagedtype.bool)]     public static extern boolean swsetgetregkeyvalue(intptr pgetregkeyvalue);      static void main(string[] args)     {         regfunction dele = getregkeyvalue;         intptr  ptr = marshal.getfunctionpointerfordelegate(dele);         gc.keepalive(dele);         bool bsetregkeyvalueok = swsetgetregkeyvalue(ptr);     }      public static int getregkeyvalue(string pregkey, string pbuffer, int ibufsize)     {        console.writeline("successful in calling method");        return 0;     } } 


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -