loading native dll's - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

loading native dll's

SpaceMan1234SpaceMan1234 Posts: 5Member
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.