Re: I have a managed DLL code written in C#

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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 [.NET/C# MVP]" wrote:

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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx


"Par" <Par@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:6E12C8AC-2EF2-4805-A89D-6B38D404A436@xxxxxxxxxxxxxxxx
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.



.



Relevant Pages

  • Re: Integrating with legacy code
    ... Removed nochkclr.obj as a linker input ... Extention DLL. ... access them from within the web service, ... >order to add managed code to it: ...
    (microsoft.public.dotnet.general)
  • Re: Strange file not found exception
    ... Assemblies *must* reside in same directory. ... DLL on Windows Server 2003 x64 using PIA interop. ... I find creating a COM coclass object in managed code is ok, ...
    (microsoft.public.vc.language)
  • Dependency walker msvcr80d.dll missing coredll.dll and dwmapi.dll
    ... I've completed migrating one of our company's C++ Managed Code ... The dll is written in MFC with sections of C++ ... The dll exports its objects in the same manner as any VS2005 Solution Wizard ... In fact when I peer into the dll with the DepedencyWalker app, ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Error using P/Invoke with Winsock API using CF.NET
    ... You aren't going to be able to pass a callback function from managed code, ... It might be significantly easier to create a native DLL ... setting up the socket in secure mode, ... >>> public static extern int ioctlsocket(int s, uint controlCode, object ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: create a wrapped managed dll that can be used from pure unmanaged code
    ... > code which calls into managed code" and some of those unmanaged ... Exporting a class based interface from a DLL ... public void SayHello() ... GCHandle gch; ...
    (microsoft.public.dotnet.languages.vc)