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 / April 2005

Tip: Looking for answers? Try searching our database.

C Sharp/.Net equiv for CoMarshalInterface

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bill - 23 Apr 2005 01:20 GMT
I have been using CoMarshalInterface and its reverse in C++ to convert a COM
interface
pointer to a string that I could pass to an ActiveX control, and also to
pass an interface back
from the control to my C++ program.  (The reason is to do with limitations
of ActiveX
control access under the SmartDocument API).  I now want to do the same from
C#.
My question is:  are there equivalent functions to the CoMarshalInterface
API accessible
from C#, and if so, how do I get at them.
"Peter Huang" [MSFT] - 23 Apr 2005 04:00 GMT
Hi

Although in the low lever, the CLR will call the CoMarshalInterface to do
interface marshal, but it is not exposed to the C# or VB.NET ...
programming language in the managed world.
So far I think you may try to use the Managed C++  to write a wrap class
for C# use to do the job.

Hope this helps.

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Bill - 24 Apr 2005 06:20 GMT
Hi Peter,

Thanks for the reply.  It does help a little, in that I now know that I can
stop
searching for a managed equivalent to CoMarshalInterface.  However, taking
up your idea of using Managed C++ to write a wrapper class, I hit another
barrier.  The samples I have examined (eg:  the MCppWrapper example
available from
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample/html/v
csammcppwrappersampledemonstrateswrappingcdllwithmanagedextensions.asp
)
show how to marshal some types between managed and unmanaged code.
So far I havn't found an example that marshals a COM pointer.

To implement a wrapper I (think I) require a way of getting unmanaged
COM pointers from and to RCW references.  So, a managed C++ routine
looking like this:

   String MyMarshal(Document *doc) { ... }

with Document being a reference to a MS Word Document will be entered with
doc being a reference to a RCW.  To use CoMarshalInterface I need to extract
the/a raw COM pointer from doc.   Subsequently, to unmarshal an interface I
will need to convert a raw COM pointer back to a RCW reference.

  Document *MyUnmarshal(String st) { ... }

Cheers
Bill

> Hi
>
[quoted text clipped - 13 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
Bill - 26 Apr 2005 11:02 GMT
Again Hi Peter,

I"ve realised that I cannot do anything in Managed C++ because it
works through RCW's in the same way as C#.  Instead I have written
an unmanaged C++ COM server (from which CoMarshalInterface
worked) and can call it from C#, wherein the conversion to raw COM
pointer happens by magic somewhere inside the RCW.  It all seems
to work ok now thanks

Cheers
Bill

> Hi Peter,
>
[quoted text clipped - 4 lines]
> barrier.  The samples I have examined (eg:  the MCppWrapper example
> available from

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample/html/v
csammcppwrappersampledemonstrateswrappingcdllwithmanagedextensions.asp
)
> show how to marshal some types between managed and unmanaged code.
> So far I havn't found an example that marshals a COM pointer.
[quoted text clipped - 33 lines]
> > This posting is provided "AS IS" with no warranties, and confers no
> rights.
"Peter Huang" [MSFT] - 27 Apr 2005 06:26 GMT
Hi Bill,

I am glat that you have figure your question our yourselves.
Yes, as your said if we write pure managed code in Managed C++, it will do
the same job with C#.(They are all based on CLR).

Also Managed C++ supported mixed mode code, that is to say, we can include
managed code together with unmanaged code( which is all in MCPP).
i.e. we call CoMarshalInterface in Unmanaged portion in the managed C++
Dll, while call the Unmanaged  portion from Managed portion in the MCPP Dll.

e.g. call the api in managed class directly.
#include <windows.h>
#using <mscorlib.dll>  //complied with /clr

__gc class MyTestWrap
{
public:
    void ShowMessageBox()
    {
        MessageBox(NULL,"Hello","World",MB_OK);
    }
    System::String* GetModuleName()
    {
        HMODULE hInst = GetModuleHandle(NULL);
        char hName[MAX_PATH];
        DWORD sz=MAX_PATH;
        if(GetModuleFileName(hInst,hName,sz)==0)
            return new System::String("");
        else
            return new System::String(hName);
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    MyTestWrap* pObject = new MyTestWrap();
    pObject->ShowMessageBox();
    System::Console::WriteLine(pObject->GetModuleName());
    return 0;
}

Also your approach is a solution too.

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Ollie Riches - 26 Apr 2005 09:43 GMT
have you checked out the namespace

System.Runtime.InteropServices.Marshal

> I have been using CoMarshalInterface and its reverse in C++ to convert a COM
> interface
[quoted text clipped - 7 lines]
> API accessible
> from C#, and if so, how do I get at them.
Bill - 26 Apr 2005 10:57 GMT
Yes

> have you checked out the namespace
>
[quoted text clipped - 13 lines]
> > API accessible
> > from C#, and if so, how do I get at them.

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.