473,396 Members | 1,893 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,396 software developers and data experts.

[DllImport(@Runtime)]

I'm sill learning VS .NET 2003, not an expert yet.

I'm calling an unmanaged C++ DLL from C# using [DllImport()]. When the
whole project is done I will be calling a total of 5 C++ DLLs from C#. All
the DLLs have the same signature, and I never have to call more than one DLL
in the same program. (The DLLs parse and validate 5 different binary
formats.) Originally I was expecting to have 5 different C# programs as the
constants are different for each of 5 DLLs. But I figured out how to
dyanmically load the constants from XML files so now one C# program is all
that's needed. ;-) Color me 'Very Happy.'

What's the best way to late bind to an unmanaged DLL from C# code?

1. Although I find no MSDN or Google samples, it seems to me that one
'should' be able to (somehow) use a string in the class constructor.
[DllImport(string DllName)]
After all, the DLL does not have to be present for the C# build, and if I
forget to copy the Dll to the Debug folder the C# program runs fine up until
the call to the Dll. It's obviously late binding to the DLL so if e.g.
DllImportAttribute.DllFilename was exposed it could easily specified during
class construction. This desireable feature appears to have been left out
of .NET 2003.

2. Various newgroups threads suggest using Reflection. This is yet another
..NET feature I know exists but haven't studied.

3. This article purports to have an assembly language solution of 4
statements.
http://www.codeproject.com/csharp/dyninvok.asp

Thanks.

-- Mark
Nov 17 '05 #1
3 3433
Mark,

For .NET 1.1 and before, the best solution is to have the five DllImport
declarations, except you would specify a different managed method name for
the name of the function in the dll. Then, you would map this method name
to your format.

You then call the appropriate managed method (which is the DllImport
method) based on your format.

You don't have to worry about all five dlls being loaded. The dll is
loaded the first time it is called, and if the other four are not called,
then they are not loaded.

In .NET 2.0, you can create a delegate which has the signature of your
function, and then assign a function pointer that you have loaded (through
LoadLibrary and then GetProcAddress) to that delegate to be called.
However, for something this specific, it's not really worth it when you can
map the five dlls/methods easily to your formats.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Jerde" <Ma*******@newsgroup.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I'm sill learning VS .NET 2003, not an expert yet.

I'm calling an unmanaged C++ DLL from C# using [DllImport()]. When the
whole project is done I will be calling a total of 5 C++ DLLs from C#.
All the DLLs have the same signature, and I never have to call more than
one DLL in the same program. (The DLLs parse and validate 5 different
binary formats.) Originally I was expecting to have 5 different C#
programs as the constants are different for each of 5 DLLs. But I figured
out how to dyanmically load the constants from XML files so now one C#
program is all that's needed. ;-) Color me 'Very Happy.'

What's the best way to late bind to an unmanaged DLL from C# code?

1. Although I find no MSDN or Google samples, it seems to me that one
'should' be able to (somehow) use a string in the class constructor.
[DllImport(string DllName)]
After all, the DLL does not have to be present for the C# build, and if I
forget to copy the Dll to the Debug folder the C# program runs fine up
until the call to the Dll. It's obviously late binding to the DLL so if
e.g. DllImportAttribute.DllFilename was exposed it could easily specified
during class construction. This desireable feature appears to have been
left out of .NET 2003.

2. Various newgroups threads suggest using Reflection. This is yet
another .NET feature I know exists but haven't studied.

3. This article purports to have an assembly language solution of 4
statements.
http://www.codeproject.com/csharp/dyninvok.asp

Thanks.

-- Mark

Nov 17 '05 #2
Nicholas -- Thanks for the reply. I think I understand. ;-) So someone
can correct me if I'm wrong, and for the possible future benefit of
googlers, this is what I have done. My original class was of this form:

namespace MyNamespace {
public class TestCase {
[DllImport("DLL-01")]
public static extern bool IsValid(byte[] Array, ...);
...
public bool Is_Valid(...)
return IsValid(...);
I have changed it to this form:

namespace MyNamespace {
public enum SupportedDLLs {
Sample,
DataFormat1,
DataFormat2,
...
}
public class TestCase {
[DllImport("Sample.dll", EntryPoint="IsValid")]
public static extern bool Sample_IsValid(byte[] Array, ...);
[DllImport("DataFormat1.dll", EntryPoint="IsValid")]
public static extern bool Format1_IsValid(byte[] Array, ...);
[DllImport("DataFormat2.dll", EntryPoint="IsValid")]
public static extern bool Format2_IsValid(byte[] Array, ...);
...
SupportedDLLs m_DLL = SupportedDLLs.Sample;
public SupportedDLLs Dll {
get { return m_DLL; }
set { m_DLL = value; }
}

public bool Is_Valid(...)
switch (m_DLL) {
case SupportedDLLs.Sample:
return Sample_IsValid(...
...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
Mark,

For .NET 1.1 and before, the best solution is to have the five
DllImport declarations, except you would specify a different managed
method name for the name of the function in the dll. Then, you would map
this method name to your format.

You then call the appropriate managed method (which is the DllImport
method) based on your format.

You don't have to worry about all five dlls being loaded. The dll is
loaded the first time it is called, and if the other four are not called,
then they are not loaded.

In .NET 2.0, you can create a delegate which has the signature of your
function, and then assign a function pointer that you have loaded (through
LoadLibrary and then GetProcAddress) to that delegate to be called.
However, for something this specific, it's not really worth it when you
can map the five dlls/methods easily to your formats.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Jerde" <Ma*******@newsgroup.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I'm sill learning VS .NET 2003, not an expert yet.

I'm calling an unmanaged C++ DLL from C# using [DllImport()]. When the
whole project is done I will be calling a total of 5 C++ DLLs from C#.
All the DLLs have the same signature, and I never have to call more than
one DLL in the same program. (The DLLs parse and validate 5 different
binary formats.) Originally I was expecting to have 5 different C#
programs as the constants are different for each of 5 DLLs. But I
figured out how to dyanmically load the constants from XML files so now
one C# program is all that's needed. ;-) Color me 'Very Happy.'

What's the best way to late bind to an unmanaged DLL from C# code?

1. Although I find no MSDN or Google samples, it seems to me that one
'should' be able to (somehow) use a string in the class constructor.
[DllImport(string DllName)]
After all, the DLL does not have to be present for the C# build, and if I
forget to copy the Dll to the Debug folder the C# program runs fine up
until the call to the Dll. It's obviously late binding to the DLL so if
e.g. DllImportAttribute.DllFilename was exposed it could easily specified
during class construction. This desireable feature appears to have been
left out of .NET 2003.

2. Various newgroups threads suggest using Reflection. This is yet
another .NET feature I know exists but haven't studied.

3. This article purports to have an assembly language solution of 4
statements.
http://www.codeproject.com/csharp/dyninvok.asp

Thanks.

-- Mark


Nov 17 '05 #3
Mark,

Yep, that's pretty much how I would do it. As long as the signatures
are the same, this will work just fine for you.

If you find that the number of formats increases at a rapid clip, then
using .NET 2.0 and the method I described with the delegates will be a
better solution, since it won't require a recompile of the code.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Jerde" <Ma*******@newsgroup.nospam> wrote in message
news:eC**************@TK2MSFTNGP14.phx.gbl...
Nicholas -- Thanks for the reply. I think I understand. ;-) So someone
can correct me if I'm wrong, and for the possible future benefit of
googlers, this is what I have done. My original class was of this form:

namespace MyNamespace {
public class TestCase {
[DllImport("DLL-01")]
public static extern bool IsValid(byte[] Array, ...);
...
public bool Is_Valid(...)
return IsValid(...);
I have changed it to this form:

namespace MyNamespace {
public enum SupportedDLLs {
Sample,
DataFormat1,
DataFormat2,
...
}
public class TestCase {
[DllImport("Sample.dll", EntryPoint="IsValid")]
public static extern bool Sample_IsValid(byte[] Array, ...);
[DllImport("DataFormat1.dll", EntryPoint="IsValid")]
public static extern bool Format1_IsValid(byte[] Array, ...);
[DllImport("DataFormat2.dll", EntryPoint="IsValid")]
public static extern bool Format2_IsValid(byte[] Array, ...);
...
SupportedDLLs m_DLL = SupportedDLLs.Sample;
public SupportedDLLs Dll {
get { return m_DLL; }
set { m_DLL = value; }
}

public bool Is_Valid(...)
switch (m_DLL) {
case SupportedDLLs.Sample:
return Sample_IsValid(...
...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP14.phx.gbl...
Mark,

For .NET 1.1 and before, the best solution is to have the five
DllImport declarations, except you would specify a different managed
method name for the name of the function in the dll. Then, you would map
this method name to your format.

You then call the appropriate managed method (which is the DllImport
method) based on your format.

You don't have to worry about all five dlls being loaded. The dll is
loaded the first time it is called, and if the other four are not called,
then they are not loaded.

In .NET 2.0, you can create a delegate which has the signature of your
function, and then assign a function pointer that you have loaded
(through LoadLibrary and then GetProcAddress) to that delegate to be
called. However, for something this specific, it's not really worth it
when you can map the five dlls/methods easily to your formats.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Jerde" <Ma*******@newsgroup.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I'm sill learning VS .NET 2003, not an expert yet.

I'm calling an unmanaged C++ DLL from C# using [DllImport()]. When the
whole project is done I will be calling a total of 5 C++ DLLs from C#.
All the DLLs have the same signature, and I never have to call more than
one DLL in the same program. (The DLLs parse and validate 5 different
binary formats.) Originally I was expecting to have 5 different C#
programs as the constants are different for each of 5 DLLs. But I
figured out how to dyanmically load the constants from XML files so now
one C# program is all that's needed. ;-) Color me 'Very Happy.'

What's the best way to late bind to an unmanaged DLL from C# code?

1. Although I find no MSDN or Google samples, it seems to me that one
'should' be able to (somehow) use a string in the class constructor.
[DllImport(string DllName)]
After all, the DLL does not have to be present for the C# build, and if
I forget to copy the Dll to the Debug folder the C# program runs fine up
until the call to the Dll. It's obviously late binding to the DLL so if
e.g. DllImportAttribute.DllFilename was exposed it could easily
specified during class construction. This desireable feature appears to
have been left out of .NET 2003.

2. Various newgroups threads suggest using Reflection. This is yet
another .NET feature I know exists but haven't studied.

3. This article purports to have an assembly language solution of 4
statements.
http://www.codeproject.com/csharp/dyninvok.asp

Thanks.

-- Mark



Nov 17 '05 #4

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

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.