By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,967 Members | 843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,967 IT Pros & Developers. It's quick & easy.

Multiplatform & Interop

P: n/a
If I write a pure C# application, only using the standart publi API, no
interop, the same binary should work well on32 bit, 64 bits and perhaps on
the compact framework as well if I link against it.

Now how could I achieve the same thing if I do interop and I have a managed
C++ API?

let say I have a managed C++ module compiled for 3 different target but with
always the same interface:
mycppmodule32.dll
mycppmodule64.dll
mycppmoduleCE.dll

let say in the C# code I also do some interop
internal class User32 // & CE ?!
{
[DllImport("USER32")]
public IntPtr GetHdc(IntPtr hWnd); // 4 byte IntPtr
}
internal class User64 // not sure it exists, but I assume....
{
[DllImport("USER64")]
public IntPtr GetHdc(IntPtr hWnd); // 8 byte IntPtr
}
Let suppose these DLLs are included as modules in my csharpdll.dll.

Now if I deploy all these DLLs, how could write a simple nice code in my C#
dll which internally target whatever native lib is appropriate?
Is it possible (to keep it simple)?
--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
Jun 20 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hi Lloyd!

Consider preparing multiple builds of your C# dll for different platforms,
using conditional compilation directives and constants to include the right
DllImport attributes. Something like:

#if WIN64
[DllImport("user64", ...]
#else
[DllImport("user32", ...]
#endif

"Lloyd Dupont" <net.galador@ld> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
If I write a pure C# application, only using the standart publi API, no
interop, the same binary should work well on32 bit, 64 bits and perhaps on
the compact framework as well if I link against it.

Now how could I achieve the same thing if I do interop and I have a
managed C++ API?

let say I have a managed C++ module compiled for 3 different target but
with always the same interface:
mycppmodule32.dll
mycppmodule64.dll
mycppmoduleCE.dll

let say in the C# code I also do some interop
internal class User32 // & CE ?!
{
[DllImport("USER32")]
public IntPtr GetHdc(IntPtr hWnd); // 4 byte IntPtr
}
internal class User64 // not sure it exists, but I assume....
{
[DllImport("USER64")]
public IntPtr GetHdc(IntPtr hWnd); // 8 byte IntPtr
}
Let suppose these DLLs are included as modules in my csharpdll.dll.

Now if I deploy all these DLLs, how could write a simple nice code in my
C# dll which internally target whatever native lib is appropriate?
Is it possible (to keep it simple)?
--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>


Jun 20 '06 #2

P: n/a

"Lloyd Dupont" <net.galador@ld> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
If I write a pure C# application, only using the standart publi API, no
interop, the same binary should work well on32 bit, 64 bits and perhaps on
the compact framework as well if I link against it.

Now how could I achieve the same thing if I do interop and I have a
managed C++ API?

let say I have a managed C++ module compiled for 3 different target but
with always the same interface:
mycppmodule32.dll
mycppmodule64.dll
mycppmoduleCE.dll

let say in the C# code I also do some interop
internal class User32 // & CE ?!
{
[DllImport("USER32")]
public IntPtr GetHdc(IntPtr hWnd); // 4 byte IntPtr
}
internal class User64 // not sure it exists, but I assume....
{
[DllImport("USER64")]
public IntPtr GetHdc(IntPtr hWnd); // 8 byte IntPtr
}
Let suppose these DLLs are included as modules in my csharpdll.dll.

Now if I deploy all these DLLs, how could write a simple nice code in my
C# dll which internally target whatever native lib is appropriate?
Is it possible (to keep it simple)?


Hi Lloyd!

IntPtr is platform dependent, so in your sample, you don't have to declare
twice.

I know, this requires a lot of rereading docs before you can be sure, it
works on both platforms. See also DMytro's answer.

Jun 20 '06 #3

P: n/a
Hi,
"Lloyd Dupont" <net.galador@ld> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
If I write a pure C# application, only using the standart publi API, no
interop, the same binary should work well on32 bit, 64 bits and perhaps on
the compact framework as well if I link against it.


In the CF you have a VERY limited set of classes and methods inside the
included classes, so the above is not 100% accurate.

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jun 20 '06 #4

P: n/a
Hi Dmytro,

Perhaps I didn't explained my self correctly.

What I was trying to do in a simple manner (if possible) is to have one
library to run them all!

Like imagine my program is a click once application, I would like (if
possible) to avoid the x32 link, the x64 link, the CE link.
Just one link and it runs whatever the end-user machine.

Or also imagine the user "install" (i.e. copy) the file on one PC, and then
he simply copy the file again on an other PC and it still works!

Conditional compilation is a clean way to have one build for each given
platform.
But I wonder if there is a no hassel way to have one build for all platform
that the user could simply copy without worrying about his pc
architecture....

Do you see what I mean?
Is it possible? (in a simple way)

For exemple a while back I wrote something like the code below so that my
pocket application, using some WinCE specific dll, runs on both the desktop
and CE smoothly.
But it's kind of overkill for a big API.

enum Platform
{
CE,
Win32,
}
public class OSUtil
{
internal static Platform Platform { get {} }

public int AMethod()
{
switch(Platform)
{
case CE:
return OSUtilCE.AMethod();
case Win32:
return OSUtilWin32.AMethod();
}
}
}
class OSUtilCE
{
[DllImport("CEDll")]
static int AMethodImpl();
static int AMethod() { return AMEthodImpl(); }
}
class OSUtil32
{
[DllImport("Win32Dll")]
static int AMethodImpl();
static int AMethod() { return AMEthodImpl(); }
}



"Dmytro Lapshyn [MVP]" <x-****@no-spam-please.hotpop.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Hi Lloyd!

Consider preparing multiple builds of your C# dll for different platforms,
using conditional compilation directives and constants to include the
right DllImport attributes. Something like:

#if WIN64
[DllImport("user64", ...]
#else
[DllImport("user32", ...]
#endif

"Lloyd Dupont" <net.galador@ld> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
If I write a pure C# application, only using the standart publi API, no
interop, the same binary should work well on32 bit, 64 bits and perhaps
on the compact framework as well if I link against it.

Now how could I achieve the same thing if I do interop and I have a
managed C++ API?

let say I have a managed C++ module compiled for 3 different target but
with always the same interface:
mycppmodule32.dll
mycppmodule64.dll
mycppmoduleCE.dll

let say in the C# code I also do some interop
internal class User32 // & CE ?!
{
[DllImport("USER32")]
public IntPtr GetHdc(IntPtr hWnd); // 4 byte IntPtr
}
internal class User64 // not sure it exists, but I assume....
{
[DllImport("USER64")]
public IntPtr GetHdc(IntPtr hWnd); // 8 byte IntPtr
}
Let suppose these DLLs are included as modules in my csharpdll.dll.

Now if I deploy all these DLLs, how could write a simple nice code in my
C# dll which internally target whatever native lib is appropriate?
Is it possible (to keep it simple)?
--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>

Jun 20 '06 #5

P: n/a
The trick, of course, is to build against the Compact CF.
Then it runs on the desktop!

Do not worry for I have developed PocketPC application :-)

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:eu**************@TK2MSFTNGP05.phx.gbl...
Hi,
"Lloyd Dupont" <net.galador@ld> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
If I write a pure C# application, only using the standart publi API, no
interop, the same binary should work well on32 bit, 64 bits and perhaps
on the compact framework as well if I link against it.


In the CF you have a VERY limited set of classes and methods inside the
included classes, so the above is not 100% accurate.

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Jun 20 '06 #6

P: n/a
mhhhh....

perhaps I could still do as below but generate this wrapper code
automatically?
that's an idea!
enum Platform
{
CE,
Win32,
}
public class OSUtil
{
internal static Platform Platform { get {} }

public int AMethod()
{
switch(Platform)
{
case CE:
return OSUtilCE.AMethod();
case Win32:
return OSUtilWin32.AMethod();
}
}
}
class OSUtilCE
{
[DllImport("CEDll")]
static int AMethodImpl();
static int AMethod() { return AMEthodImpl(); }
}
class OSUtil32
{
[DllImport("Win32Dll")]
static int AMethodImpl();
static int AMethod() { return AMEthodImpl(); }
}

Jun 20 '06 #7

P: n/a
Lloyd,
Let suppose these DLLs are included as modules in my csharpdll.dll.


Why do you want to deploy that way?

Personally I would, if possible, have three builds of a single library
mycppmodule.dll, and have the installer make sure the correct version
gets installed on the system. That way a single set of DllImports can
work on all platforms (assuming you can write all the signatures in a
way that works everywhere).
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 21 '06 #8

P: n/a
My problem is requiring an installer to have the library working seems
awfully overkill.
I like the simple "xcopy" deployment method and I wonder if I could achieve
it while being multiplatform...
Well I huess it's not really pratical...
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:eR**************@TK2MSFTNGP02.phx.gbl...
Lloyd,
Let suppose these DLLs are included as modules in my csharpdll.dll.


Why do you want to deploy that way?

Personally I would, if possible, have three builds of a single library
mycppmodule.dll, and have the installer make sure the correct version
gets installed on the system. That way a single set of DllImports can
work on all platforms (assuming you can write all the signatures in a
way that works everywhere).
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jun 22 '06 #9

P: n/a
Lloyd Dupont wrote:
My problem is requiring an installer to have the library working seems
awfully overkill.
I like the simple "xcopy" deployment method and I wonder if I could achieve
it while being multiplatform...
Well I huess it's not really pratical...


Just in case you didn't check there:
I suggested a possible workaround in
microsoft.public.dotnet.framework.interop,
Jun 22 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.