Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2006

Tip: Looking for answers? Try searching our database.

Multiplatform & Interop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lloyd Dupont - 20 Jun 2006 03:12 GMT
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)?

Signature

Regards,
Lloyd Dupont

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

Dmytro Lapshyn [MVP] - 20 Jun 2006 09:39 GMT
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

> 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
[quoted text clipped - 26 lines]
> C# dll which internally target whatever native lib is appropriate?
> Is it possible (to keep it simple)?
Lloyd Dupont - 20 Jun 2006 16:30 GMT
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(); }
}

> Hi Lloyd!
>
[quoted text clipped - 38 lines]
>> C# dll which internally target whatever native lib is appropriate?
>> Is it possible (to keep it simple)?
Lloyd Dupont - 20 Jun 2006 16:35 GMT
mhhhh....

perhaps I could still do as below but generate this wrapper code
automatically?
that's an idea!

> enum Platform
> {
[quoted text clipped - 28 lines]
>   static int AMethod() { return AMEthodImpl(); }
> }
Dmytro Lapshyn [MVP] - 21 Jun 2006 10:09 GMT
Well, you should be able to generate an "interop" assembly dynamically and
emit the IL code and the attributes on the fly. But it is much more headache
than having a number of builds for different platforms.

> mhhhh....
>
[quoted text clipped - 34 lines]
>>   static int AMethod() { return AMEthodImpl(); }
>> }
Egbert Nierop (MVP for IIS) - 20 Jun 2006 10:34 GMT
> 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
[quoted text clipped - 26 lines]
> 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.
Ignacio Machin ( .NET/ C# MVP ) - 20 Jun 2006 14:51 GMT
Hi,

> 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.

Signature

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

Lloyd Dupont - 20 Jun 2006 16:33 GMT
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 :-)

> Hi,
>
[quoted text clipped - 4 lines]
> In the CF you have a VERY limited set of classes and methods inside the
> included classes, so the above is not 100% accurate.
Mattias Sjögren - 21 Jun 2006 21:57 GMT
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

Signature

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

Lloyd Dupont - 22 Jun 2006 07:00 GMT
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...

> Lloyd,
>
[quoted text clipped - 9 lines]
>
> Mattias
Christian Fröschlin - 22 Jun 2006 08:30 GMT
> 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,

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.