c# - Attempted to read write protected memory -
i have c++ dll exports method this:
extern "c" __declspec (dllexport) void conve(int type, const char* path, int b1, int b2, int b3, int b4, int b5) { ffilelist file_list; char temp_path[1024]; if(type == 1) { sprintf(temp_path,"%s*",path); getfindfilelistwin(temp_path,".mrs",file_list); file_list.recoveryzipe(b1, b2, b3, b4, b5); file_list.convertnamemres2zip(); } else if(type == 2) { sprintf(temp_path,"%s*",path); getfindfilelistwin(temp_path,".zip",file_list); file_list.convertzipe(b5, b4, b3, b2, b1); file_list.convertnamezip2mres(); } } and i'm calling c# application this:
[dllimport("mrs.dll", charset = charset.ansi, callingconvention = callingconvention.cdecl)] public static extern void conve(int type, string path, int b1, int b2, int b3, int b4, int b5); but everytime run it, throws me error "attempt read or write protected memory indicating other memory corrupt"
as far read, there's wrong way i'm importing function in c#, don't know how solve it.
edit: default, there conv() same conve without 5 integers, , 1 works fine. made conve use file_list.recoveryzipe() when debugging, stacks me on method inside 1 called recvoerychare (i added code, can take it) , also, 1 base on recoverychar() works on conv().
recoverychar:
void recoverychar(char* pdata,int _size) { if(!pdata) return; byte b,bh,d; for(int i=0;i<_size;i++) { b = *pdata; bh = b&0x07; d = (bh<<5)|(b>>3); *pdata = d ^ 0xff; pdata++; } } recvoerychare:
void recoverychare(char* pdata, int _size, int b1, int b2, int b3, int b4, int b5) { if(!pdata) return; byte b; for(int i=0;i<_size;i++) { b = *pdata; b = (((((b >> b1) | (b << 5)) ^ b2) + b3) ^ b4) - b5; *pdata = b; pdata++; } } last exception got when debugging c# app: 
edit: after debugging again, stucks on here
recoverychare( _fileheaderreader , _fileheaderreadersize, b1, b2, b3, b4, b5 ); for reason, b1 value taking 0 always.
, seems reason i'm getting protected memory error.
all did copy methods recoverychar() , convertchar() present , pass values parameters.
i explore things:
what platform on c++ , on c# side ? both 32-bits ? both 64-bits ? may have issue parameters transfer between 2 languages done wrong if have mix of integer length. anyway safest bet specify exact integer width using instance 'int32_t' instead of 'int'
some people suggest specifiying exact marshaller when converting c++ 'char*' c# 'string' using instance "marshalas(unmanagedtype.lpstr)] string path", please try this
finally add "printf-like" debugging code in c++ dump values @ various steps (including @ begining of call) debugging console. can use following function used printf outputs debuging console.
code sample debug output in c++:
inline void log_(const char *format, ...) { va_list args; va_start(args, format); char buffer[1000]; vsprintf(buffer, format, args); outputdebugstringa(buffer); }
Comments
Post a Comment