By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,161 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,161 IT Pros & Developers. It's quick & easy.

Need help understanding how to call unmanaged dll

P: n/a
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?

Feb 3 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 3 '07 #2

P: n/a
Hi,

"mjohnson" <cr****@hotmail.comwrote in message
news:11*********************@v45g2000cwv.googlegro ups.com...
>
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 :)
Feb 4 '07 #3

P: n/a
"mjohnson" <cr****@hotmail.comwrote in message
news:11*********************@v45g2000cwv.googlegro ups.com...
>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.
Feb 4 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.