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 / Interop / June 2005

Tip: Looking for answers? Try searching our database.

Calling C# from C++, need to late-bind to the C# object.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
clintp@gmail.com - 06 Jun 2005 21:20 GMT
My C++ isn't so good, so excuse the terminology mixups.

I have a C# COM application.  Following examples and documentation, I
can get the application so that it's callable from C++ if I embed
something like this in my C++ program:

#import "\ComServer\obj\Debug\ComServer.tlb" no_namespace named_guids

and then use:

       CoInitialize(NULL);
    HRESULT hr = CoCreateInstance(
               CLSID_InterfaceImplementation,
        NULL, CLSCTX_INPROC_SERVER,
        IID_IManagedInterface, reinterpret_cast<void**>
                  &cpi));
        // Etc..

To call it.  The problem is that I need to build this C++ application
WITHOUT HAVING THE .TLB FILE HANDY.  I have the C# using a fixed GUID,
so I would think there'd be a reasonable way to have the C++ code find
the DLL using that?  Or be able to extract out the correct headers for
the C# interface so that I could just paste them into the C++?
Phil Wilson - 06 Jun 2005 22:15 GMT
If you really mean late binding, then it's IDispatch you should be using.
Assuming a registered ProgID, class registration, IDispatch-capable server
etc, it's something like this to call a method named GetMyString, no
parameters, with late binding COM (and this is what VBScript does under the
covers too):

CoInitialize(0);
HRESULT hr = 0;
CLSID cls;
IDispatch* getit=NULL;
hr = CLSIDFromProgID (L"my.progid", &cls);
hr = CoCreateInstance (cls, NULL, CLSCTX_INPROC_SERVER,
__uuidof(IDispatch), (void**)&getit);
if (SUCCEEDED(hr))
{
 VARIANT VarResult = {0};
 DISPID dispid;
 DISPPARAMS  dispparamsNoArgs = {NULL, NULL, 0, 0};
 LPOLESTR  szMember = OLESTR("GetMyString");
 hr = getit->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT,
&dispid);
 hr = getit->Invoke(dispid, IID_NULL,
  LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dispparamsNoArgs, &VarResult,
NULL, NULL);
 getit->Release();
}

Signature

Phil Wilson [MVP Windows Installer]
----

> My C++ isn't so good, so excuse the terminology mixups.
>
[quoted text clipped - 19 lines]
> the DLL using that?  Or be able to extract out the correct headers for
> the C# interface so that I could just paste them into the C++?
clintp@gmail.com - 07 Jun 2005 14:23 GMT
This is *exactly* what I was looking for.  Hunting around on the 'Net
without that keyword "IDispatch" was getting me lots and lots of
useless answers.

Thanks!

Rate this thread:







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.