Need help understanding how to call unmanaged dll

M

mjohnson

I bought a USB IR reciever and installed the driver. The developer
kit gives a DLL with the following information:
C++ Builder / Microsoft Visual C++:

#ifdef __cplusplus
extern "C" {
#endif

int __stdcall DoGetInfraCode(uchar * code, int extra, int *
codeLen);

#ifdef __cplusplus
}
#endif

So in my C# project I have done this:

public class IgorUSB
{
[DllImport("IgorUSB.dll")]
public static extern int DoGetInfraCode(ref byte[] code, int
extra, ref int codeLen);
}
Which I assume it the correct way to define the wrapper to the dll.

Next I added this to my Windows Form:

private void Form1_Load(object sender, EventArgs e)
{
byte[] code = new byte[12];
int extra = 0;
int len = 0;
IgorUSB.DoGetInfraCode(ref code, extra, ref len);
}

But I get the following error when I run it:

"LoaderLock was detected
Message: Attempting managed execution inside OS Loader lock. Do not
attempt to run managed code inside a DllMain or image initialization
function since doing so can cause the application to hang."

Does my code look correct? The only thing I can think of is the when
this DLL is loaded it pops up a message box. Could that be the
problem?
 
M

Mattias Sjögren

int __stdcall DoGetInfraCode(uchar * code, int extra, int *
codeLen);

#ifdef __cplusplus
}
#endif

So in my C# project I have done this:

public class IgorUSB
{
[DllImport("IgorUSB.dll")]
public static extern int DoGetInfraCode(ref byte[] code, int
extra, ref int codeLen);
}
Which I assume it the correct way to define the wrapper to the dll.


You should remove the ref modifier on the first parameter.

Not sure why that would cause a loader lock error though.


Mattias
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

But I get the following error when I run it:

"LoaderLock was detected
Message: Attempting managed execution inside OS Loader lock. Do not
attempt to run managed code inside a DllMain or image initialization
function since doing so can cause the application to hang."

Does my code look correct? The only thing I can think of is the when
this DLL is loaded it pops up a message box. Could that be the
problem?


A buffer overflow?

This is the first time I read that error.

Of course, there is always the possibility that the dll is corrupted and not
working fine :)
 
W

Willy Denoyette [MVP]

mjohnson said:
I bought a USB IR reciever and installed the driver. The developer
kit gives a DLL with the following information:
C++ Builder / Microsoft Visual C++:

#ifdef __cplusplus
extern "C" {
#endif

int __stdcall DoGetInfraCode(uchar * code, int extra, int *
codeLen);

#ifdef __cplusplus
}
#endif

So in my C# project I have done this:

public class IgorUSB
{
[DllImport("IgorUSB.dll")]
public static extern int DoGetInfraCode(ref byte[] code, int
extra, ref int codeLen);
}
Which I assume it the correct way to define the wrapper to the dll.

Next I added this to my Windows Form:

private void Form1_Load(object sender, EventArgs e)
{
byte[] code = new byte[12];
int extra = 0;
int len = 0;
IgorUSB.DoGetInfraCode(ref code, extra, ref len);
}

But I get the following error when I run it:

"LoaderLock was detected
Message: Attempting managed execution inside OS Loader lock. Do not
attempt to run managed code inside a DllMain or image initialization
function since doing so can cause the application to hang."

Does my code look correct? The only thing I can think of is the when
this DLL is loaded it pops up a message box. Could that be the
problem?


Your code is correct, the DLL's code is wrong.
The reason for the LoaderLock MDA is that the DLL class into another Win32 function that
requires an OS Loader lock.
Check this for more info:
http://msdn2.microsoft.com/en-us/library/ms172219.aspx

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top