473,624 Members | 2,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("test um.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 9066

"Greg Roberts" <no****@nospam. com> wrote in message
news:OQ******** *****@tk2msftng p13.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("test um.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:MarshalA s(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.int ermediate.manif est & SampleC.dll.emb ed.manifest
and seem to have successfully added a reference
in the C# project refs to SampleC.dll.emb ed.manifest but now get link
errors

Warning 1 Assembly '..\debug\Sampl eC.dll.embed.ma nifest' has mismatched
identity 'SampleC', expected file name: 'SampleC.manife st'. UnitTestofCShar p

My unmanaged settings manifest line is
/nologo /identity:"Sampl eC" /out:".\Debug\Sa mpleC.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******** ******@TK2MSFTN GP12.phx.gbl...
"Greg Roberts" <no****@nospam. com> wrote in message
news:OQ******** *****@tk2msftng p13.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("test um.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:MarshalA s(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******** *****@TK2MSFTNG P12.phx.gbl...
| Thanks for the feedback
|
| I am having trouble trying to get the manifest part correct.
|
| I have found SampleC.dll.int ermediate.manif est &
SampleC.dll.emb ed.manifest
| and seem to have successfully added a reference
| in the C# project refs to SampleC.dll.emb ed.manifest but now get link
| errors
|
| Warning 1 Assembly '..\debug\Sampl eC.dll.embed.ma nifest' has mismatched
| identity 'SampleC', expected file name: 'SampleC.manife st'.
UnitTestofCShar p
|
| My unmanaged settings manifest line is
| /nologo /identity:"Sampl eC" /out:".\Debug\Sa mpleC.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******** ******@TK2MSFTN GP12.phx.gbl...
| >
| > "Greg Roberts" <no****@nospam. com> wrote in message
| > news:OQ******** *****@tk2msftng p13.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("test um.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:MarshalA s(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******** ******@TK2MSFTN GP10.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******** *****@TK2MSFTNG P12.phx.gbl...
| Thanks for the feedback
|
| I am having trouble trying to get the manifest part correct.
|
| I have found SampleC.dll.int ermediate.manif est &
SampleC.dll.emb ed.manifest
| and seem to have successfully added a reference
| in the C# project refs to SampleC.dll.emb ed.manifest but now get link
| errors
|
| Warning 1 Assembly '..\debug\Sampl eC.dll.embed.ma nifest' has mismatched
| identity 'SampleC', expected file name: 'SampleC.manife st'.
UnitTestofCShar p
|
| My unmanaged settings manifest line is
| /nologo /identity:"Sampl eC" /out:".\Debug\Sa mpleC.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******** ******@TK2MSFTN GP12.phx.gbl...
| >
| > "Greg Roberts" <no****@nospam. com> wrote in message
| > news:OQ******** *****@tk2msftng p13.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("test um.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:MarshalA s(UnmanagedType .Bool)] int someFunction(.. ..)
| >
| > Check MSDN for details on PInvoke marshaling.
| >
| > Willy.
| >
| >
|
|

Mar 2 '06 #5

"Greg Roberts" <gr*********@NO SPAMLOWLIFEcite ct.com> wrote in message
news:OO******** ******@TK2MSFTN GP10.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("simp le.dll")]
extern static int Empty();
static void Main()
{
Console.WriteLi ne(Empty());
}

[build.cmd]

cl /LD /MD /EHsc simple.cpp
mt -manifest simple.dll.mani fest /nologo /outputresource: "simple.dll ;#2"
del simple.dll.mani fest
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.d ll and SampleC.dll lived in !! These are in a common
debug or release directory

Go figure !!

It would seem the process starts from "UnitTesting.vs host.exe" which loads
and calls the MSTestProject.d ll
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.DllNotF oundException' occurred in
MSTestProject.d ll

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 'LoadFromContex t' has detected a problem in
'E:\Program Files\Microsoft Visual Studio 8\Common7\IDE\v stesthost.exe'.
Additional Information: The assembly named
'Microsoft.Visu alStudio.Qualit yTools.Executio nCommon' was loaded from
'file:///E:/Program Files/Microsoft Visual Studio
8/Common7/IDE/PrivateAssembli es/Microsoft.Visua lStudio.Quality Tools.Execution Common.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******** ******@TK2MSFTN GP10.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.d ll and SampleC.dll lived in !! These are in a common
| debug or release directory
|
| Go figure !!
|
| It would seem the process starts from "UnitTesting.vs host.exe" which loads
| and calls the MSTestProject.d ll
| 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.DllNotF oundException' occurred
in
| MSTestProject.d ll
|
| 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 'LoadFromContex t' has detected a problem in
| 'E:\Program Files\Microsoft Visual Studio 8\Common7\IDE\v stesthost.exe'.
| Additional Information: The assembly named
| 'Microsoft.Visu alStudio.Qualit yTools.Executio nCommon' was loaded from
| 'file:///E:/Program Files/Microsoft Visual Studio
|
8/Common7/IDE/PrivateAssembli es/Microsoft.Visua lStudio.Quality Tools.Execution Common.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
947
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 type in C# or VB.NET) be garbage collected? Similarily, if C# or VB.NET pass SAFEARRAY to the COM object via COM method invocation, will this SAFEARRAY be garbage collected after the call? My guess to answers for both questions is yes. Thanks.
1
1937
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 easy language that is built into the system. It is supposed to be capable of extending itself by calling external DLLs. It is very picky about what DLLs it will see though Others have had success using C++ to write a DLL that will work with Easy...
11
1330
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 VB6 and .NET application (both are running) - done benchmark: 1) XML dom - VERY BAD 2) Interop - BAD 3) Pointers - GOOD and fast, but BAD (unmanaged) Any ideas how to do it/???
1
2115
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. The guts will remain in unmanaged c++. So I need to write public managed c++ wrapper classes for the couple of interfaces the unmanaged stuff needs to expose for the GUI. Questions:
1
1107
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 de-serializes objects to/from XML. But, I want to use this from an unmanaged vc app that I am writing. Several questions: is it possible to create managed static library that can then be linked to my unmanaged app at build time; or must I use a...
0
1033
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 COM implements a collection of mathematical calculations and algorithms. The COM object, when requested, should be able to render a graph representing it’s calculations to a window, to achieve that it expose a
1
1554
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 best tutorial (pubished work or web site link) on how to make sense of how to do this... A simple example is the PlaySound WINAPI method, which looks like this BOOL WINAPI PlaySound( LPCSTR pszSound, HMODULE hmod,
5
1893
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 send the .exe to the users without needing to send the dll. Is it possible to build my project without the accompanying dll ?
5
3269
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 ubiquitously takes a float* which represents an array of floats. If the memory is allocated by the client, in C# or VB.NET, I assume it must be "fixed" so that the GC does not move the pointer during the C algorithm. Given that we are choosing between C#...
0
8251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8688
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8352
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7178
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1496
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.