Solved

Calling managed code (Microsoft office interop) from unmanged c+= cde

Posted on 2011-03-10
5
636 Views
Last Modified: 2012-06-21
I need to call Microsoft.office.interop.word.dll from an older c++ appliacion. (Written with Visual Studio 6) .
I have read several articcles athat say you can call a managed library from unmanaged code but I cannot find an example or walkthrough on it. I have also rad of "Mixed Mode images" solving this problem but have no examples of implementing it.

Any samples would be greatly applreciated.
0
Comment
Question by:jkcompute
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 35097079
Well, the title of the following article might be a bit misleading, but it's a detailed walkthorugh to what you want to do: http://www.codeproject.com/KB/COM/ComHard.aspx ("COM Interop the Hard Way") - COM Interop (http://msdn.microsoft.com/en-us/library/kew41ycz%28VS.71%29.aspx - "Introduction to COM Interop") allows you to call .NET assemblies as COM objects from unmanaged code.
0
 
LVL 4

Expert Comment

by:parnasso
ID: 35097245
If you just want a C++ console application, you can do it by creating a .NET runtime host, instantiate any type of Microsoft.office.interop.word.dll and use it entirely in C++ code, no COM needed.

Find an example in the following Microsoft sample application:

CppHostCLR
0
 
LVL 32

Expert Comment

by:sarabande
ID: 35107578
for mixed code assemblies you may search for IJW (it just works) what was the slogan used by ms regarding those assemblies.

Sara
0
 

Author Comment

by:jkcompute
ID: 35148922
I went with JKR's solution, except Iwimped out and made it real easy.  I decided to just write a managed app to convert rtf files to doc files as a way to start.  This is critical because my main app is a windows service and real hard to degug.  Microsoft Interop word functions are a nightmare with security because there is a bug in the reg APPID keys tht show up if installl Word before the framework, there are also some versionissues if you do not develop on themachine that will run the service.  to make a long story short, I got the app mostly running.  Instead of converting it to a dll file, I simly call it from within my service, passing command line params instead of function params.  It is work great. (I go for fujctional and not programming purity).

I still have one small problem that I am hoping someone can shed light on.  Code samples for C++ interop are very rare and hard to find, so I translate from VB.  A vp command that works in other apps that I wrote is  
     Dim formatDOC As Object = Word.WdSaveFormat.wdFormatDocument

I have translated it to
Microsoft::Office::Interop::Word::WdSaveFormat*    formatRTF = new   Microsoft::Office::Interop::Word::WdSaveFormat::wdFormatDocument    ;
or simply (both should work)
Word::WdSaveFormat* formatR = new Word::WdSaveFormat::wdFormatDocument;
These line bring the error message -
error C2061: syntax error : identifier 'wdFormatDocument'
I have checked spelling a dozen times.

I will accept JKR's solution after waitng a few days to see if I get any new comments on this translation issue.
       
0
 

Author Closing Comment

by:jkcompute
ID: 35165267
Thanks
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now