Dll Import problem

J

James

I have a Dll written in C. And I want to Import that Dll into my C#
program and use a function in the Dll. In a previous newsgroup article
I saw that I may need to wrap the Dll in a C++ .NET class and then use
that in my C# application. But when I run my program I get an error
saying

System.IO.FileNotFoundException: File or assembly name "abc", or one
of its dependencies, was not found.
File name: "abc"

abc is my wrapper project class library.

here is my wrapper class is given below. The lib name is wdll.lib and
the header is dll.h.


#include "dll.h"

#pragma comment(lib, "wdll.lib")

namespace MyWrapper {
public __gc class Class1 {
public:

static void myfunc1(long JobNo)
{
_libmain(JobNo);
}

static char * myfunc2(void *vJobRec, short field, short
subfield)
{
return (GETJF_NAME( vJobRec ));
}

};
}

Here is the actual dll header file.

extern "C" {

void WINAPI _libmain ( long JobNo );
void * WINAPI _getjobfield ( void *vJobRec, short field, short
subfield );

};

......

and here is where I am calling it.


using MyWrapper;

unsafe
{
Class1.myfunc2(null,0,0);
}

It throws the exception here,
System.IO.FileNotFoundException: File or assembly name "abc", or one
of its dependencies, was not found.
File name: "abc"

________________________________________________


So then I tried to acces the Dll in a more traditional way , but would
result in the same error. Here is just a simple console application
that I did,

using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;
using MyWrapper;

namespace test
{
class Class1
{
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
static extern IntPtr LoadLibrary(string lpFileName);

[DllImport("wdll.dll")]
public static extern IntPtr
_getjobfield([MarshalAs(UnmanagedType.LPStr)] string s, short i1,
short i2);

[STAThread]
static void Main(string[] args)
{
// Esure current directory is exe directory
Environment.CurrentDirectory = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location );

string dllPath = Path.GetFullPath(
@"C:\SoftwareDevelopment\testing\testing\test\test" );
LoadLibrary( dllPath );
Console.WriteLine( _getjobfield(null,0,0) );

}
}
}

But it throws the exception, Can any one help.

Thanks
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi James,
You don't have to write a wrapper classes in C++. Since you have plain
c-style functions you can use P\Invoke to use them directly.

If you have installed MSDN library you can find more info here:
ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconconsumingunmanageddllfun
ctions.htm

The same article can be found on-line at:
http://msdn.microsoft.com/library/d.../html/cpconConsumingUnmanagedDLLFunctions.asp

However, take a look at DllImport attribute and Marshal class. You will need
to do some (could be significant) work to write managed equivalents of some
structures that you migh need to have in order to make those function calls.

What VC++ gives you is that this is the only .NET language (the only one
that I'm aware of except ILAssembler) that offers the ability to write mixed
(managed and unmanaged) code. So you can do that. You can use the dll how
you had used it before and export a managed classes at the same time. But
mixed assemblies are not verifiable so you have to have permisions to use
them. However the same permisson story goes and if you use PInvoke from C#
application.

Anyway, you don't have to go thru managed extension of VC++ if you don't
feel yourself comfortable whit it.

--
HTH
B\rgds
100

James said:
I have a Dll written in C. And I want to Import that Dll into my C#
program and use a function in the Dll. In a previous newsgroup article
I saw that I may need to wrap the Dll in a C++ .NET class and then use
that in my C# application. But when I run my program I get an error
saying

System.IO.FileNotFoundException: File or assembly name "abc", or one
of its dependencies, was not found.
File name: "abc"

abc is my wrapper project class library.

here is my wrapper class is given below. The lib name is wdll.lib and
the header is dll.h.


#include "dll.h"

#pragma comment(lib, "wdll.lib")

namespace MyWrapper {
public __gc class Class1 {
public:

static void myfunc1(long JobNo)
{
_libmain(JobNo);
}

static char * myfunc2(void *vJobRec, short field, short
subfield)
{
return (GETJF_NAME( vJobRec ));
}

};
}

Here is the actual dll header file.

extern "C" {

void WINAPI _libmain ( long JobNo );
void * WINAPI _getjobfield ( void *vJobRec, short field, short
subfield );

};

.....

and here is where I am calling it.


using MyWrapper;

unsafe
{
Class1.myfunc2(null,0,0);
}

It throws the exception here,
System.IO.FileNotFoundException: File or assembly name "abc", or one
of its dependencies, was not found.
File name: "abc"

________________________________________________


So then I tried to acces the Dll in a more traditional way , but would
result in the same error. Here is just a simple console application
that I did,

using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;
using MyWrapper;

namespace test
{
class Class1
{
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
static extern IntPtr LoadLibrary(string lpFileName);

[DllImport("wdll.dll")]
public static extern IntPtr
_getjobfield([MarshalAs(UnmanagedType.LPStr)] string s, short i1,
short i2);

[STAThread]
static void Main(string[] args)
{
// Esure current directory is exe directory
Environment.CurrentDirectory = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location );

string dllPath = Path.GetFullPath(
@"C:\SoftwareDevelopment\testing\testing\test\test" );
LoadLibrary( dllPath );
Console.WriteLine( _getjobfield(null,0,0) );

}
}
}

But it throws the exception, Can any one help.

Thanks
 

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