A
antarikshv
I am loading a native dll, i.e. a dll created using native code. My
development environment is VC++ .Net 2.0, managed code.
The code is as follows,
The function signature in the dll is:
int CalculateKey(BYTE *seed, int seedLength, BYTE *key, int
keyLength);
the delegate declaration in my code is:
delegate int CalculateKey(int * seed, int seedLength,int * key, int
keyLength);
the code for loading of the dll and using its function:
OpenFileDialog ^ of = gcnew OpenFileDialog();
of->ShowDialog();
IntPtr hExe = LoadLibrary(of->FileName);
if (hExe.ToInt32() == 0)
{
MessageBox::Show("Cannot open " + of->FileName);
}
GCHandle ^ gch = GCHandle::Alloc(of->FileName,GCHandleType:inned); //
convert object to handle
IntPtr hProc = GetProcAddress(hExe,"CalculateKey");
if(hProc.ToInt32() == 0)
{
MessageBox::Show("Not Found");
}
CalculateKey ^ ObjCalcFunc = (CalculateKey ^)
Marshal::GetDelegateForFunctionPointer(hExe,CalculateKey::typeid);
array<char> ^ uSeed = gcnew array<char>{1,2,3,4};
array<char> ^ uKey = gcnew array<char>(4);
pin_ptr<char> pinptrp = &uSeed[0];
pin_ptr<char> pinptrp1 = &uKey[0];
int * ppp = reinterpret_cast<int *>(pinptrp);
int * ppp1 = reinterpret_cast<int *>(pinptrp1);
ObjCalcFunc(ppp,uSeed->Length,ppp1,uKey->Length);
With the above code i am facing the exception when the ObjCalcFunc is
called as
"An unhandled exception of type 'System.AccessViolationException'
occurred in tesbed.exe
Additional information: Attempted to read or write protected memory.
This is often an indication that other memory is corrupt."
Now i have searched various sites for finding a solution to this, but
haven't succeeded yet. Please let me know the solution.
Thanks in advance.
development environment is VC++ .Net 2.0, managed code.
The code is as follows,
The function signature in the dll is:
int CalculateKey(BYTE *seed, int seedLength, BYTE *key, int
keyLength);
the delegate declaration in my code is:
delegate int CalculateKey(int * seed, int seedLength,int * key, int
keyLength);
the code for loading of the dll and using its function:
OpenFileDialog ^ of = gcnew OpenFileDialog();
of->ShowDialog();
IntPtr hExe = LoadLibrary(of->FileName);
if (hExe.ToInt32() == 0)
{
MessageBox::Show("Cannot open " + of->FileName);
}
GCHandle ^ gch = GCHandle::Alloc(of->FileName,GCHandleType:inned); //
convert object to handle
IntPtr hProc = GetProcAddress(hExe,"CalculateKey");
if(hProc.ToInt32() == 0)
{
MessageBox::Show("Not Found");
}
CalculateKey ^ ObjCalcFunc = (CalculateKey ^)
Marshal::GetDelegateForFunctionPointer(hExe,CalculateKey::typeid);
array<char> ^ uSeed = gcnew array<char>{1,2,3,4};
array<char> ^ uKey = gcnew array<char>(4);
pin_ptr<char> pinptrp = &uSeed[0];
pin_ptr<char> pinptrp1 = &uKey[0];
int * ppp = reinterpret_cast<int *>(pinptrp);
int * ppp1 = reinterpret_cast<int *>(pinptrp1);
ObjCalcFunc(ppp,uSeed->Length,ppp1,uKey->Length);
With the above code i am facing the exception when the ObjCalcFunc is
called as
"An unhandled exception of type 'System.AccessViolationException'
occurred in tesbed.exe
Additional information: Attempted to read or write protected memory.
This is often an indication that other memory is corrupt."
Now i have searched various sites for finding a solution to this, but
haven't succeeded yet. Please let me know the solution.
Thanks in advance.