loading native dll's

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:
[color=Blue]int CalculateKey(BYTE *seed, int seedLength, BYTE *key, int keyLength);[/color]

the delegate declaration in my code is:

[color=Green]delegate int CalculateKey(int * seed, int seedLength,int * key, int keyLength);[/color]

the code for loading of the dll and using its function:

[color=Purple]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::Pinned); //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 ^ uSeed = gcnew array{1,2,3,4};
array ^ uKey = gcnew array(4);
pin_ptr pinptrp = &uSeed[0];
pin_ptr pinptrp1 = &uKey[0];
int * ppp = reinterpret_cast(pinptrp);
int * ppp1 = reinterpret_cast(pinptrp1);[/color]
[size=4][color=Red]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."[/color][/size]

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.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories