I have a managed DLL code written in C#

G

Guest

Hi

I have a managed DLL code written in C#, which is calling / importing an
unmanaged DLL function. The function in the undamaged DLL code looks like

- Void delete_tuner (short tuner_number)

I create one class that shall contain a set of related unmanaged DLL
functions. The class I created looks like

public class Dll_Pool

{
public DllImportAttribute My_dll = new DllImportAttribute(dllName);

[DllImport(@"\\obj\\Release\\MLibTuners.dll")]

public static extern void delete_tuner(Int16 tuner_number);
public string GetDllName()
{
string dllName;
dllName = My_dll.Value;
return dllName;
}

public Dll_Pool()

{

}


public static void DllMain()

{
Int16 tuner_number = 0;
---> delete_tuner(tuner_number); <--- // The executing of the
code is stoped here and I got an error message

}
}


I have even a simple C# test code which is calling the imported function
according to

Dll_Pool.DllMain();

I have no problem to build the code and start running it in the debug mode.
As I show between the arrows in the code above the executing of the code is
stopped with error message saying.
"Loader Lock Was detected 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."

I found information, under help documentation which describes what Loader
Lock means and the reasons of it but I did not find documentation in how to
solve the problem. I really hope someone could help me to solve the problem.
 
N

Nicholas Paldino [.NET/C# MVP]

Par,

I am curious why you have this line:

public DllImportAttribute My_dll = new DllImportAttribute(dllName);

There is no way this code can compile, as dllName doesn't exist in your
class definition. Also, decaring the attribute like that does nothing for
you.

Also, for the name of the dll, you need to give it a DLL name/path which
will be searched by the LoadLibrary API. Using
"\obj\Release\MLibTuners.dll" is probably not a good idea, since that is
probably not where the dll is going to be when you distribute the program.

Can you create just a console program which makes the call to the static
method, and does it work?
 
G

Guest

Hi Nicholas

Please ignore the code I sent. The main question now is how to import an
unmanaged DLL code into a managed code. When I import and run this unmanaged
DLL code into my own test application I get an error message saying the
“Loader Lock was detectedâ€.

The “DllImport†attribute is used to import the unmanaged code according to

[DllImport("MLibTuners.dll", CharSet = CharSet.Auto, EntryPoint =
"delete_tuner")]

public static extern void delete_tuner(short tuner_number);

Thanks


--
P L


Nicholas Paldino said:
Par,

I am curious why you have this line:

public DllImportAttribute My_dll = new DllImportAttribute(dllName);

There is no way this code can compile, as dllName doesn't exist in your
class definition. Also, decaring the attribute like that does nothing for
you.

Also, for the name of the dll, you need to give it a DLL name/path which
will be searched by the LoadLibrary API. Using
"\obj\Release\MLibTuners.dll" is probably not a good idea, since that is
probably not where the dll is going to be when you distribute the program.

Can you create just a console program which makes the call to the static
method, and does it work?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Par said:
Hi

I have a managed DLL code written in C#, which is calling / importing an
unmanaged DLL function. The function in the undamaged DLL code looks like

- Void delete_tuner (short tuner_number)

I create one class that shall contain a set of related unmanaged DLL
functions. The class I created looks like

public class Dll_Pool

{
public DllImportAttribute My_dll = new DllImportAttribute(dllName);

[DllImport(@"\\obj\\Release\\MLibTuners.dll")]

public static extern void delete_tuner(Int16 tuner_number);
public string GetDllName()
{
string dllName;
dllName = My_dll.Value;
return dllName;
}

public Dll_Pool()

{

}


public static void DllMain()

{
Int16 tuner_number = 0;
---> delete_tuner(tuner_number); <--- // The executing of the
code is stoped here and I got an error message

}
}


I have even a simple C# test code which is calling the imported function
according to

Dll_Pool.DllMain();

I have no problem to build the code and start running it in the debug
mode.
As I show between the arrows in the code above the executing of the code
is
stopped with error message saying.
"Loader Lock Was detected 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."

I found information, under help documentation which describes what Loader
Lock means and the reasons of it but I did not find documentation in how
to
solve the problem. I really hope someone could help me to solve the
problem.
 
G

Guest

Hi Nicholas

Please ignore the code. The main question now is how to import an unmanaged
DLL code into a managed code. When I import and run this unmanaged DLL code
into my own test application I get an error message saying the “Loader Lock
was detectedâ€.

The “DllImport†attribute is used to import the unmanaged code according to


[DllImport("MLibTuners.dll", CharSet = CharSet.Auto, EntryPoint =
"delete_tuner")]

public static extern void delete_tuner(short tuner_number);

Thanks


--
P L


Nicholas Paldino said:
Par,

I am curious why you have this line:

public DllImportAttribute My_dll = new DllImportAttribute(dllName);

There is no way this code can compile, as dllName doesn't exist in your
class definition. Also, decaring the attribute like that does nothing for
you.

Also, for the name of the dll, you need to give it a DLL name/path which
will be searched by the LoadLibrary API. Using
"\obj\Release\MLibTuners.dll" is probably not a good idea, since that is
probably not where the dll is going to be when you distribute the program.

Can you create just a console program which makes the call to the static
method, and does it work?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Par said:
Hi

I have a managed DLL code written in C#, which is calling / importing an
unmanaged DLL function. The function in the undamaged DLL code looks like

- Void delete_tuner (short tuner_number)

I create one class that shall contain a set of related unmanaged DLL
functions. The class I created looks like

public class Dll_Pool

{
public DllImportAttribute My_dll = new DllImportAttribute(dllName);

[DllImport(@"\\obj\\Release\\MLibTuners.dll")]

public static extern void delete_tuner(Int16 tuner_number);
public string GetDllName()
{
string dllName;
dllName = My_dll.Value;
return dllName;
}

public Dll_Pool()

{

}


public static void DllMain()

{
Int16 tuner_number = 0;
---> delete_tuner(tuner_number); <--- // The executing of the
code is stoped here and I got an error message

}
}


I have even a simple C# test code which is calling the imported function
according to

Dll_Pool.DllMain();

I have no problem to build the code and start running it in the debug
mode.
As I show between the arrows in the code above the executing of the code
is
stopped with error message saying.
"Loader Lock Was detected 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."

I found information, under help documentation which describes what Loader
Lock means and the reasons of it but I did not find documentation in how
to
solve the problem. I really hope someone could help me to solve the
problem.
 

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