DllImport not found problem

B

belion

Hi,

I am facing a strange problem ... a collegue of mine made two
projects. The first one is an unmanaged C++ dll , and the other is a
C# managed project that uses the previous dll. Both of them are
working on his machine, but not in mine. When I run the C# project, I
always get a DllNotFoundException, even it the path to the dll is well
set (I have checked it). I have also tried to copy the dll to C:
\WINDOWS\system32 , but the same problem arises.

Some lines of code.
The important part from the C++ dll

#define NAG_CALL __stdcall
#define NAG_DLL_EXPIMP __declspec(dllexport)


BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

extern "C"
{
NAG_DLL_EXPIMP void NAG_CALL MallocImage(Image *img)
{
img->imageData =
(char *)malloc((size_t)(sizeof(char)*img->imageSize));

}
}


And in the C# project

[DllImport(@"C:\Projects\code\OpenCvBridge\TrialBridge\Debug
\TrialBridge.dll", EntryPoint = "MallocImage")]
public static extern void MallocImage(ref Image img);
.......
Image img = new Image();
img.imageSize = 600 * 800;
MallocImage(ref img);
.......

It always fails when it arrives to MallocImage(ref img)

By the way, I checked the dll with DependencyWalker, and it does not
have additional dependencies.

Thank you,
 
D

Doug Semler

Hi,

I am facing a strange problem ... a collegue of mine made two
projects. The first one is an unmanaged C++ dll , and the other is a
C# managed project that uses the previous dll. Both of them are
working on his machine, but not in mine. When I run the C# project, I
always get a DllNotFoundException, even it the path to the dll is well
set (I have checked it). I have also tried to copy the dll to C:
\WINDOWS\system32 , but the same problem arises.

Some lines of code.
The important part from the C++ dll

#define NAG_CALL __stdcall
#define NAG_DLL_EXPIMP __declspec(dllexport)

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;

}

extern "C"
{
NAG_DLL_EXPIMP void NAG_CALL MallocImage(Image *img)
{
img->imageData =
(char *)malloc((size_t)(sizeof(char)*img->imageSize));

}

}

And in the C# project

[DllImport(@"C:\Projects\code\OpenCvBridge\TrialBridge\Debug
\TrialBridge.dll", EntryPoint = "MallocImage")]
public static extern void MallocImage(ref Image img);

Uhh, do you have a .def file being linked in the C++ project? If you
don't have a .def file, your EntryPoint should be
"_MallocImage@4" (due to the _declspec(dllexport))
 
N

Nicholas Paldino [.NET/C# MVP]

The problem is in the DllImport declaration where the dll is specified:

C:\Projects\code\OpenCvBridge\TrialBridge\Debug\TrialBridge.dll

That means that TrialBridge MUST be in a directory named:

C:\Projects\code\OpenCvBridge\TrialBridge\Debug\

It doesn't matter where else it is. If the entry was just
"TrialBridge.dll" then it would follow the rules that LoadLibrary uses when
locating the dll (see the documentation there for the algorithm taken to
find the dll). However, if it is in the same directory as the executable,
it should find it.
 
D

Doug Semler

I am facing a strange problem ... a collegue of mine made two
projects. The first one is an unmanaged C++ dll , and the other is a
C# managed project that uses the previous dll. Both of them are
working on his machine, but not in mine. When I run the C# project, I
always get a DllNotFoundException, even it the path to the dll is well
set (I have checked it). I have also tried to copy the dll to C:
\WINDOWS\system32 , but the same problem arises.
Some lines of code.
The important part from the C++ dll
#define NAG_CALL __stdcall
#define NAG_DLL_EXPIMP __declspec(dllexport)
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;

extern "C"
{
NAG_DLL_EXPIMP void NAG_CALL MallocImage(Image *img)
{
img->imageData =
(char *)malloc((size_t)(sizeof(char)*img->imageSize));


And in the C# project
[DllImport(@"C:\Projects\code\OpenCvBridge\TrialBridge\Debug
\TrialBridge.dll", EntryPoint = "MallocImage")]
public static extern void MallocImage(ref Image img);

Uhh, do you have a .def file being linked in the C++ project? If you
don't have a .def file, your EntryPoint should be
"_MallocImage@4" (due to the _declspec(dllexport))- Hide quoted text -

- Show quoted text -

Ignore that I thought I read "entrypoint not found exception <g>
 
B

belion

The problem is in the DllImport declaration where the dll is specified:
C:\Projects\code\OpenCvBridge\TrialBridge\Debug\TrialBridge.dll

That means that TrialBridge MUST be in a directory named:
Yes, that is the idea, and that's where the dll is. But as I did not
find it, I tried to copy it into C:\WINDOWS\system32 and of course,
the changed the import to:

[DllImport("TrialBridge.dll", EntryPoint = "MallocImage")]

Anyway, it keeps on failing.
 
N

Nicholas Paldino [.NET/C# MVP]

Is it the same error that you get? A DllNotFoundException, or do you
get a different error now?

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

The problem is in the DllImport declaration where the dll is
specified:

C:\Projects\code\OpenCvBridge\TrialBridge\Debug\TrialBridge.dll

That means that TrialBridge MUST be in a directory named:
Yes, that is the idea, and that's where the dll is. But as I did not
find it, I tried to copy it into C:\WINDOWS\system32 and of course,
the changed the import to:

[DllImport("TrialBridge.dll", EntryPoint = "MallocImage")]

Anyway, it keeps on failing.
 
W

Willy Denoyette [MVP]

Always the same error: DllNotFoundException,


That means you are missing a dependency, my guess is that the VC runtime
DLL's (debug and retail) are missing, these are loaded dynamically if you
module isn't statically linked.

Willy.
 
L

Leon Lambert

A missing dependency sounds right here. Try making a small C++ console
application that links to the dll. It will give a better error on what
is missing.

Hope this helps.
leon Lambert
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Leon said:
A missing dependency sounds right here. Try making a small C++ console
application that links to the dll. It will give a better error on what
is missing.

DependencyWalker or similar tool would be even faster.

Arne
 
W

Willy Denoyette [MVP]

Arne Vajhøj said:
DependencyWalker or similar tool would be even faster.

Arne


But Dependency walker cannot show dynamically loaded modules unless you are
able to profile the application ( from within dependency walker). This means
that you must run the application which is not possible when the missing
module(s) is/are one of the C run-time dll's.

Willy.
 

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