Muhmmad Aziz
Muhmmad Aziz

Reputation: 391

Integrate DLL into .Net project

I'm trying to import DLL files into my project, I already tried 'Add reference' and it didn't work:

'a reference to *.dll could not be added. please make sure that the file is accessible, and that it is a valid assembly or com component'

I tried to register the dll using 'regsvr32' and it didn't work:

'the module *.dll was loaded but the entry-point DllRegisterServer was not found'

finally I used DllImport, (I'm currently testing with libclamav.dll)

lImport("libclamav.dll")]
public static extern int cl_scanfile(String path);
private void button1_Click(object sender, EventArgs e)
{
 string path="e:\\scan\\111.jpg";
 int n;
 n = cl_scanfile(path);
}

but I have an error on execution:

'An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)'

Any suggestions??

Upvotes: 0

Views: 769

Answers (1)

ScheuNZ
ScheuNZ

Reputation: 911

It sounds like you want to p/Invoke methods in an unmanaged DLL. You should have a read through the Platform Invoke Tutorial provided by Microsoft and see if that helps.

  • You generally only add references to other managed .NET assemblies. If the DLL you're trying to use isn't a managed DLL, this probably isn't what you're looking for.
  • You generally only register a DLL with regsvr32 if you intend to consume a COM API. If the DLL you're trying to consume isn't a COM DLL, this probably isn't what you're looking for.
  • You p/Invoke if you're trying to consume an unmanaged / non-COM dll and want to directly call the unmanaged functions within it. This is probably what you're trying to do based on my quick Google for libclamav.dll.

If you're getting that incorrect format exception, this is possibly because your .NET application is targeting 'Any CPU' but the DLL you're trying to load is 32 bit DLL (and you're on a 64 bit machine). If this is the case, you might want to set the 'Platform Target' of your .NET application to 'x86' and see if that helps.

To successfully p/Invoke, you also need to have the DLL you're calling into (and all of its dependencies) in the search path somewhere. The easiest way to ensure this is to copy the DLL and all of the dependencies into the bin\Debug or bin\Release folder of your .NET application and see if that helps.

Upvotes: 1

Related Questions