473,378 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Some interop Questions - C# to unmanaged DLL

Hi

1. Is there any way to access a static function in a class via the
[DllImport] syntax ?
e.g. in C#

[DllImport("testum.dll")]
static extern void MyClass::MyFunc();

The compiler does not like the class reference ...
Of course a pure static function is OK ...

2. When the C# is run, i get a
"The specified module could not be found. (Exception from HRESULT:
0x8007007E)"
for the DLL, even though testum.dll has been exported at build time to the
same directory as the C# program.
Dependency Walker shows the function is in testum.dll .
Tips on what to do ?


3. Can specific [marshall.xxxx] be done on the return value ? I have seen
examples showing this for the
the function arguments, but have not seen one with marshalling on the return
value below the [DLLImport ...] statement.

Thanks in advance

NB: I should mention that the C# program is an actual VS2005 team system
test project
Mar 1 '06 #1
7 9053

"Greg Roberts" <no****@nospam.com> wrote in message
news:OQ*************@tk2msftngp13.phx.gbl...
| Hi
|
| 1. Is there any way to access a static function in a class via the
| [DllImport] syntax ?
| e.g. in C#
|
| [DllImport("testum.dll")]
| static extern void MyClass::MyFunc();
|
| The compiler does not like the class reference ...
| Of course a pure static function is OK ...
|

No, you can't call C++ class methods from C#, you can only call non member
functions.

| 2. When the C# is run, i get a
| "The specified module could not be found. (Exception from HRESULT:
| 0x8007007E)"
| for the DLL, even though testum.dll has been exported at build time to the
| same directory as the C# program.
| Dependency Walker shows the function is in testum.dll .
| >> Tips on what to do ?
|

That indicates that your DLL or one of it's dependecies aren't found
(probably a dependency).
When using VC8 (vs2005) make sure you embed the assembly manifest, or you'll
have to statically link the CRT, failing to do one of these will throw the
same error when loading the DLL.

| 3. Can specific [marshall.xxxx] be done on the return value ? I have seen
| examples showing this for the
| the function arguments, but have not seen one with marshalling on the
return
| value below the [DLLImport ...] statement.
|

[return:MarshalAs(UnmanagedType.Bool)] int someFunction(....)

Check MSDN for details on PInvoke marshaling.

Willy.
Mar 1 '06 #2
Thanks for the feedback

I am having trouble trying to get the manifest part correct.

I have found SampleC.dll.intermediate.manifest & SampleC.dll.embed.manifest
and seem to have successfully added a reference
in the C# project refs to SampleC.dll.embed.manifest but now get link
errors

Warning 1 Assembly '..\debug\SampleC.dll.embed.manifest' has mismatched
identity 'SampleC', expected file name: 'SampleC.manifest'. UnitTestofCSharp

My unmanaged settings manifest line is
/nologo /identity:"SampleC" /out:".\Debug\SampleC.dll.embed.manifest"
/notify_update

The object browser for the SampleC entry can see everything

The unmanaged DLL is a non MFC dynamic linked library
Any futher tips of an URL of this working sucessfully would be
appreciated

Thanks
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:ec**************@TK2MSFTNGP12.phx.gbl...
"Greg Roberts" <no****@nospam.com> wrote in message
news:OQ*************@tk2msftngp13.phx.gbl...
| Hi
|
| 1. Is there any way to access a static function in a class via the
| [DllImport] syntax ?
| e.g. in C#
|
| [DllImport("testum.dll")]
| static extern void MyClass::MyFunc();
|
| The compiler does not like the class reference ...
| Of course a pure static function is OK ...
|

No, you can't call C++ class methods from C#, you can only call non member
functions.

| 2. When the C# is run, i get a
| "The specified module could not be found. (Exception from HRESULT:
| 0x8007007E)"
| for the DLL, even though testum.dll has been exported at build time to
the
| same directory as the C# program.
| Dependency Walker shows the function is in testum.dll .
| >> Tips on what to do ?
|

That indicates that your DLL or one of it's dependecies aren't found
(probably a dependency).
When using VC8 (vs2005) make sure you embed the assembly manifest, or
you'll
have to statically link the CRT, failing to do one of these will throw the
same error when loading the DLL.

| 3. Can specific [marshall.xxxx] be done on the return value ? I have
seen
| examples showing this for the
| the function arguments, but have not seen one with marshalling on the
return
| value below the [DLLImport ...] statement.
|

[return:MarshalAs(UnmanagedType.Bool)] int someFunction(....)

Check MSDN for details on PInvoke marshaling.

Willy.

Mar 1 '06 #3
You don't have to set a reference to the manifest file, all you have to do
is set "Embed Manifest " to true in your project settings(see Manifest
Tool - Input And Output).
The manifest tool (MT.EXE) will embed the manifest into the assembly when
done.

Willy.
"Greg Roberts" <no****@nospam.com> wrote in message
news:eS*************@TK2MSFTNGP12.phx.gbl...
| Thanks for the feedback
|
| I am having trouble trying to get the manifest part correct.
|
| I have found SampleC.dll.intermediate.manifest &
SampleC.dll.embed.manifest
| and seem to have successfully added a reference
| in the C# project refs to SampleC.dll.embed.manifest but now get link
| errors
|
| Warning 1 Assembly '..\debug\SampleC.dll.embed.manifest' has mismatched
| identity 'SampleC', expected file name: 'SampleC.manifest'.
UnitTestofCSharp
|
| My unmanaged settings manifest line is
| /nologo /identity:"SampleC" /out:".\Debug\SampleC.dll.embed.manifest"
| /notify_update
|
| The object browser for the SampleC entry can see everything
|
| The unmanaged DLL is a non MFC dynamic linked library
|
| >> Any futher tips of an URL of this working sucessfully would be
| >> appreciated
|
| Thanks
|
|
| "Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
| news:ec**************@TK2MSFTNGP12.phx.gbl...
| >
| > "Greg Roberts" <no****@nospam.com> wrote in message
| > news:OQ*************@tk2msftngp13.phx.gbl...
| > | Hi
| > |
| > | 1. Is there any way to access a static function in a class via the
| > | [DllImport] syntax ?
| > | e.g. in C#
| > |
| > | [DllImport("testum.dll")]
| > | static extern void MyClass::MyFunc();
| > |
| > | The compiler does not like the class reference ...
| > | Of course a pure static function is OK ...
| > |
| >
| > No, you can't call C++ class methods from C#, you can only call non
member
| > functions.
| >
| > | 2. When the C# is run, i get a
| > | "The specified module could not be found. (Exception from HRESULT:
| > | 0x8007007E)"
| > | for the DLL, even though testum.dll has been exported at build time to
| > the
| > | same directory as the C# program.
| > | Dependency Walker shows the function is in testum.dll .
| > | >> Tips on what to do ?
| > |
| >
| > That indicates that your DLL or one of it's dependecies aren't found
| > (probably a dependency).
| > When using VC8 (vs2005) make sure you embed the assembly manifest, or
| > you'll
| > have to statically link the CRT, failing to do one of these will throw
the
| > same error when loading the DLL.
| >
| > | 3. Can specific [marshall.xxxx] be done on the return value ? I have
| > seen
| > | examples showing this for the
| > | the function arguments, but have not seen one with marshalling on the
| > return
| > | value below the [DLLImport ...] statement.
| > |
| >
| > [return:MarshalAs(UnmanagedType.Bool)] int someFunction(....)
| >
| > Check MSDN for details on PInvoke marshaling.
| >
| > Willy.
| >
| >
|
|
Mar 1 '06 #4
Willy,

I sorted the manifest out and it links OK, Still getting the same error

Interesting if i rename the C dll so it won't be found, i get the same error
!!

At this stage i think my only hope is finding someone with a simple ZIPed
C# / C DLL sample which works to reverse engineer why it works .

I have used the DllImport before myself to get at OS Win32 DLL functions
no probs but not my own DLL ?

Thanks

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:en**************@TK2MSFTNGP10.phx.gbl...
You don't have to set a reference to the manifest file, all you have to do
is set "Embed Manifest " to true in your project settings(see Manifest
Tool - Input And Output).
The manifest tool (MT.EXE) will embed the manifest into the assembly when
done.

Willy.
"Greg Roberts" <no****@nospam.com> wrote in message
news:eS*************@TK2MSFTNGP12.phx.gbl...
| Thanks for the feedback
|
| I am having trouble trying to get the manifest part correct.
|
| I have found SampleC.dll.intermediate.manifest &
SampleC.dll.embed.manifest
| and seem to have successfully added a reference
| in the C# project refs to SampleC.dll.embed.manifest but now get link
| errors
|
| Warning 1 Assembly '..\debug\SampleC.dll.embed.manifest' has mismatched
| identity 'SampleC', expected file name: 'SampleC.manifest'.
UnitTestofCSharp
|
| My unmanaged settings manifest line is
| /nologo /identity:"SampleC" /out:".\Debug\SampleC.dll.embed.manifest"
| /notify_update
|
| The object browser for the SampleC entry can see everything
|
| The unmanaged DLL is a non MFC dynamic linked library
|
| >> Any futher tips of an URL of this working sucessfully would be
| >> appreciated
|
| Thanks
|
|
| "Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
| news:ec**************@TK2MSFTNGP12.phx.gbl...
| >
| > "Greg Roberts" <no****@nospam.com> wrote in message
| > news:OQ*************@tk2msftngp13.phx.gbl...
| > | Hi
| > |
| > | 1. Is there any way to access a static function in a class via the
| > | [DllImport] syntax ?
| > | e.g. in C#
| > |
| > | [DllImport("testum.dll")]
| > | static extern void MyClass::MyFunc();
| > |
| > | The compiler does not like the class reference ...
| > | Of course a pure static function is OK ...
| > |
| >
| > No, you can't call C++ class methods from C#, you can only call non
member
| > functions.
| >
| > | 2. When the C# is run, i get a
| > | "The specified module could not be found. (Exception from HRESULT:
| > | 0x8007007E)"
| > | for the DLL, even though testum.dll has been exported at build time
to
| > the
| > | same directory as the C# program.
| > | Dependency Walker shows the function is in testum.dll .
| > | >> Tips on what to do ?
| > |
| >
| > That indicates that your DLL or one of it's dependecies aren't found
| > (probably a dependency).
| > When using VC8 (vs2005) make sure you embed the assembly manifest, or
| > you'll
| > have to statically link the CRT, failing to do one of these will throw
the
| > same error when loading the DLL.
| >
| > | 3. Can specific [marshall.xxxx] be done on the return value ? I
have
| > seen
| > | examples showing this for the
| > | the function arguments, but have not seen one with marshalling on
the
| > return
| > | value below the [DLLImport ...] statement.
| > |
| >
| > [return:MarshalAs(UnmanagedType.Bool)] int someFunction(....)
| >
| > Check MSDN for details on PInvoke marshaling.
| >
| > Willy.
| >
| >
|
|

Mar 2 '06 #5

"Greg Roberts" <gr*********@NOSPAMLOWLIFEcitect.com> wrote in message
news:OO**************@TK2MSFTNGP10.phx.gbl...
| Willy,
|
| I sorted the manifest out and it links OK, Still getting the same error
|
| Interesting if i rename the C dll so it won't be found, i get the same
error
| !!
|

That's normal, the error is returned when the DLL (called a module) or one
of it's dependencies is not found.
As your native DLL is dynamically linked with the CRT msvcr80.dll (or it's
debyg version msvcr80d.dll), this one becomes a dependency. It's also
important to know this when you move your DLL to another box which doesn't
have VS2005 installed, the DLL will fail to load the dependent msvcr80.dll
as it's not available.

| At this stage i think my only hope is finding someone with a simple ZIPed
| C# / C DLL sample which works to reverse engineer why it works .
|

I think, the easiest is to create a simple C# program and a native dll and
build both from the command line.
[CPP]
// simple.cpp
#include <windows.h>
#include <cstdio>
extern "C" {
__declspec( dllexport ) int __stdcall Empty();
}

int __stdcall Empty() {return 0;}

[CS]
// usesimple.cs
[DllImport("simple.dll")]
extern static int Empty();
static void Main()
{
Console.WriteLine(Empty());
}

[build.cmd]

cl /LD /MD /EHsc simple.cpp
mt -manifest simple.dll.manifest /nologo /outputresource:"simple.dll;#2"
del simple.dll.manifest
csc /t:exe usesimple.cs

Willy.

Mar 2 '06 #6

Willy thanks for perservering ..

Latest update

I built a simple vc8 testc.exe console program and could load and use the
samplec.dll i have talked about .

I then used filemon.exe to trace file activity, and to my suprise the
execution path tries about 30 odd
directories (some often repeated) for the samplec.dll but NONE of them where
the directory
that MSTestProject.dll and SampleC.dll lived in !! These are in a common
debug or release directory

Go figure !!

It would seem the process starts from "UnitTesting.vshost.exe" which loads
and calls the MSTestProject.dll
which when running can't find samplec.dll

SO as a temp. measure i added the debug directory to the windows PATH env.
and restarted the vs2005 IDE

Now the process finds samplec.dll and now i have the new error set ....

"A first chance exception of type 'System.DllNotFoundException' occurred in
MSTestProject.dll

Additional information: Unable to load DLL 'SampleC.dll': This application
has failed to start because the application configuration is incorrect.
Reinstalling the application may fix this problem. (Exception from HRESULT:
0x800736B1)"
So even if i get past this issue, i don't understand why the system did
not try the directory that all the binaries where living in .


I seem to have the correct manifest settings ..

NB: While in debug and changing exceptions to always thrown, several of the
following occured before the issue above.

"Managed Debugging Assistant 'LoadFromContext' has detected a problem in
'E:\Program Files\Microsoft Visual Studio 8\Common7\IDE\vstesthost.exe'.
Additional Information: The assembly named
'Microsoft.VisualStudio.QualityTools.ExecutionComm on' was loaded from
'file:///E:/Program Files/Microsoft Visual Studio
8/Common7/IDE/PrivateAssemblies/Microsoft.VisualStudio.QualityTools.ExecutionCommo n.DLL'
using the LoadFrom context. The use of this context can result in unexpected
behavior for serialization, casting and dependency resolution. In almost all
cases, it is recommended that the LoadFrom context be avoided. This can be
done by installing assemblies in the Global Assembly Cache or in the
ApplicationBase directory and using Assembly.Load when explicitly loading
assemblies."

Thanks
Mar 3 '06 #7
Greg,

native dll's are loaded by the OS loader (using LoadLibrary Win32 API) not
by the CLR loader (fusion). The paths scanned are those defined by the
loader rules (these can be found in MSDN - search for "Dynamic-Link Library
Search Order").
Willy.
"Greg Roberts" <no****@nospam.com> wrote in message
news:ub**************@TK2MSFTNGP10.phx.gbl...
|
| Willy thanks for perservering ..
|
| Latest update
|
| I built a simple vc8 testc.exe console program and could load and use the
| samplec.dll i have talked about .
|
| I then used filemon.exe to trace file activity, and to my suprise the
| execution path tries about 30 odd
| directories (some often repeated) for the samplec.dll but NONE of them
where
| the directory
| that MSTestProject.dll and SampleC.dll lived in !! These are in a common
| debug or release directory
|
| Go figure !!
|
| It would seem the process starts from "UnitTesting.vshost.exe" which loads
| and calls the MSTestProject.dll
| which when running can't find samplec.dll
|
| SO as a temp. measure i added the debug directory to the windows PATH env.
| and restarted the vs2005 IDE
|
| Now the process finds samplec.dll and now i have the new error set ....
|
| "A first chance exception of type 'System.DllNotFoundException' occurred
in
| MSTestProject.dll
|
| Additional information: Unable to load DLL 'SampleC.dll': This application
| has failed to start because the application configuration is incorrect.
| Reinstalling the application may fix this problem. (Exception from
HRESULT:
| 0x800736B1)"
|
| >> So even if i get past this issue, i don't understand why the system did
| >> not try the directory that all the binaries where living in .
|
| I seem to have the correct manifest settings ..
|
| NB: While in debug and changing exceptions to always thrown, several of
the
| following occured before the issue above.
|
| "Managed Debugging Assistant 'LoadFromContext' has detected a problem in
| 'E:\Program Files\Microsoft Visual Studio 8\Common7\IDE\vstesthost.exe'.
| Additional Information: The assembly named
| 'Microsoft.VisualStudio.QualityTools.ExecutionComm on' was loaded from
| 'file:///E:/Program Files/Microsoft Visual Studio
|
8/Common7/IDE/PrivateAssemblies/Microsoft.VisualStudio.QualityTools.ExecutionCommo n.DLL'
| using the LoadFrom context. The use of this context can result in
unexpected
| behavior for serialization, casting and dependency resolution. In almost
all
| cases, it is recommended that the LoadFrom context be avoided. This can be
| done by installing assemblies in the Global Assembly Cache or in the
| ApplicationBase directory and using Assembly.Load when explicitly loading
| assemblies."
|
| Thanks
|
|
Mar 3 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JC | last post by:
How does garbage collection work in C# and VB.NET for data returned from COM object? For example, a COM object written in C++ returns a SAFEARRAY to C# or VB.NET, will this SAFEARRAY (mapped to...
1
by: Doug | last post by:
I need to make the VB.NET object callable from a program called Trade Station. It is a stock analysis and trading tool that is very importiant to my company. They have a scripting languge called...
11
by: Tamir Khason | last post by:
Is it possible (if 'yes' - how) to transfer data from VB application to .NET application and v.v WITHOUT using Interop (very expencive process). I need to manipulate big bunches of raw data between...
1
by: Eric Twietmeyer | last post by:
Hello, I'm starting to investigate cs, managed c++ and interoperating with a very large unmanaged code base. We are going to use Windows Forms (written in cs) to replace our old fashioned GUI. ...
1
by: S Wheeler | last post by:
This may have been previously addressed, if so sorry... I am trying to create a serialization library that will be used from unmanaged vc++. I have working vc++ managed code that serializes and...
0
by: Nadav | last post by:
Hi, Introduction: I have developed an interop project built of an unmanaged COM object and a ..NET WinForm application, the WinForm Application implement the user interface while the unmanaged...
1
by: JDeats | last post by:
I'm doing Windows Mobile 5 programming and since MS has not exposed most of the functionality i have to call DllImport a lot, I haven't done much interop with unmanaged code, I'm looking for the...
5
by: Yoavo | last post by:
Hi, In my application I have to use som external COM dll's. In the code: using MyDll when I build the project, I get (in addition to the .exe file) a Dll called Interop.MyDll.dll I want to...
5
by: John | last post by:
I would like to ask a question that is obvious to all people porting applications from the "traditional" C\VB6 interop scheme choosing C# vs VB.NET. We have a math library in C which...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.