Jump to content

COM interface C# to C

- - - - -

  • Please log in to reply
6 replies to this topic

#1
acpaps

acpaps

    Newbie

  • Members
  • Pip
  • 1 posts
Hi i am trying to implement a COM interface between C# to C

i do not have much knowledge about this...could anyone please help me

#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
are you trying to expose .NET to C? or C to .NET

#3
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
i'm heading in soon,
I guess what i was going to say, is that if you write a COM object in C, and register it, you create a wrapper that .NET can use to consume it. This can be done right through the Visual Studio Add References dialog.

If you need to use a .NET component as COM, you can create a TLB wrapper, that can be registered and consumed by C, or VB or whatever else.

but... reply or PM me if you need more info...

#4
hi5

hi5

    Newbie

  • Members
  • Pip
  • 2 posts
Hi,

I want to know how to write a Re: COM interface C# to C DLL. Can you please explain about it with some example code.

Thanks in advance

#5
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
OK, I'll talk for a second in a really general sense.
Everything you do within the .NET framework kinda runs within the virtual machine. And anything that isn't written in .NET doesn't. Thats about as technical in this light I want to get here, because that's all you really have to understand.

Whether you are writing an application in say, vb6, and you want to use a component written in .NET;
or you have a .NET application, and you want to use a COM object (like... ADODB for example)

you have to realize that you're application is going to run in multiple processes. Some of it will be managed, and some of it will not.

to use a COM object in .NET is actually exceptionally easy, without any prior knowledge at all. In fact, if you take a .NET project, and add a reference to it, you will see a dialog that allows you to add a reference to .NET objects from the GAC, other .NET projects in your solution, the ability to browse to an assembly and the ability to reference COM objects.

Selecting a registered COM object causes the IDE to automatically create a wrapper for this COM object so you can access it from within your application. So long as your application is NOT strong named, theres nothing else to it. (if your application IS signed and strong named, you must use the command line utility that generates these wrappers, so you can assign a strong name to it).

now, lets say you want the opposite. You build this KING library, in .NET, and you need to use it from VB6. The framework has this wonderful utility called Type Library Importer (Type Library Importer (Tlbimp.exe) ) which allows you to generate .tlb files (which can be registered [regsvr32 whatever.tlb], and can then be referenced from your VB app (or whatever).

I think thats a great starting point, its kinda pointless to share code, without some kind of understanding of what one is talking about. =)

A word of caution, if you're building in a corporate environment, you may need to add steps of signing/delay signing applications, or else you'll get trust related exceptions when you try and do this stuff...

#6
hi5

hi5

    Newbie

  • Members
  • Pip
  • 2 posts
Hi, Sam_Coder,
Thanks for you quick reply.
well , with your description it all makes sense. Here i am talking about another case, where you can not add DLL as a reference to a .net project and will throw exception saying assembly manifest file.
Like calling using P/Invoke . Can you explain this.
Thanks in advance.

#7
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
hey, no problem

yes, I can give this a shot. (using this: pinvoke.net: messagebox (user32) )

P/Invoke (platform invocation services) allow you to make unmanaged calls from within managed environments.

it basically involves you creating a signature like:
[DllImport("user32.dll", CharSet=CharSet.Auto)]

public static extern MessageBoxResult MessageBox(IntPtr hWnd, String text, String caption, int options);

make sure you add
using System.Runtime.InteropServices;
to the top if your code file.

from here, you can call MessageBox from within your C# code. (but as you can see by the DllImport attribute, this is actually just a prototype, mapped to the User 32 Windows API.

But what about those custom types, like MessageBoxResult.... if you planned in testing the result of that API call, you might want to map that type to make it easier to interpret. another example from that site.

/// <summary>

/// Flags that define appearance and behaviour of a standard message box displayed by a call to the MessageBox function.

/// </summary>

[Flags]

public enum MessageBoxOptions : uint

{

    Ok         = 0x000000,

    OkCancel       = 0x000001,

    AbortRetryIgnore   = 0x000002,

    YesNoCancel    = 0x000003,

    YesNo          = 0x000004,

    RetryCancel    = 0x000005,

    CancelTryContinue  = 0x000006,


    IconHand       = 0x000010,

    IconQuestion       = 0x000020,

    IconExclamation    = 0x000030,

    IconAsterisk       = 0x000040,

    UserIcon       = 0x000080,


    IconWarning    = IconExclamation,

    IconError      = IconHand,

    IconInformation    = IconAsterisk,

    IconStop       = IconHand,


    DefButton1     = 0x000000,

    DefButton2     = 0x000100,

    DefButton3     = 0x000200,

    DefButton4     = 0x000300,


    ApplicationModal   = 0x000000,

    SystemModal    = 0x001000,

    TaskModal      = 0x002000,


    Help           = 0x004000, //Help Button

    NoFocus        = 0x008000,


    SetForeground      = 0x010000,

    DefaultDesktopOnly = 0x020000,

    Topmost        = 0x040000,

    Right          = 0x080000,

    RTLReading     = 0x100000,

}


/// <summary>

/// Represents possible values returned by the MessageBox function.

/// </summary>

public enum MessageBoxResult : uint

{

    Ok = 1,

    Cancel,

    Abort,

    Retry,

    Ignore,

    Yes,

    No,

    Close,

    Help,

    TryAgain,

    Continue,

    Timeout = 32000

}

pulling it all together


using System;

using System.Runtime.InteropServices;    


class Class1

{

    [DllImport("user32.dll", CharSet=CharSet.Auto)]

    static extern int MessageBox(IntPtr hWnd, String text, String caption, int options);


    [STAThread]

    static void Main(string[] args)

    {

        MessageBox(IntPtr.Zero, "Text", "Caption", 0);

    }

}


This is a really small example mind you, I could show you a much more practical example, like using pinvokes to enable custom painting on treeview controls etc. But it just seemed to me something like this would be easier to grasp at first.

is there a particular API you're struggling with? or just curious?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users