Newbie question: COM programming and linking to Windows libraries

Patrick Kristiansen patrick.kristiansen.lists at gmail.com
Thu Mar 12 14:12:41 PDT 2009


Patrick Kristiansen Wrote:

> Hi
> 
> I've been trying out D lately, and I really love it. I use the Tango library at the moment, and I wanted to do some simple COM programming, which I'm also a newbie at, just to get more acquainted with Windows programming in D.
> 
> What I want to do is relatively simple I think. I want to use the IActiveDesktop COM interface to change the wallpaper of my desktop. So far I have ported the definition of the interface to D and the different structs used as arguments to the interface's methods. The porting went pretty easily, although I haven't been able to test it yet (I'll return to that in a moment). The standard Windows types (e.g. LPCTSTR, DWORD, etc.) are, as you probably know, defined in tango.sys.win32.Types.
> 
> I looked at the samples that is included with the DMD compiler bundle (dclient.d, dserver.d and chello.d) to see COM programming in D is done. Apparently, one of the first things to do is call CoInitialize(...), and this is where my problem arises.
> 
> CoInitialize is located in ole32.lib, but in the Tango distribution of DMD, there is no ole32.lib included. I thought, since I have the Windows SDK (x64) installed, that I could just link directly to the ole32.lib included there.
> 
> Now, this is probably obvious to some people - but not to me. Why doesn't this work? Why does OPTLINK fail and tell me that the format of the .lib file is wrong? Is it because Digital Mars compilers produce .lib files in a different format? Is it because I have the 64 bit SDK installed, and DMD and optlink only produce and consume 32 bit object files and libraries?
> 
> Please enlighten me ;-)
> 
> Thanks in advance.
> 
> -Patrick

Following the suggestions given by multiple people on this forum, I still run into trouble. Here is what I did.

First I used implib to import the OMF library for ole32.dll. I am using Windows Vista x64, and I invoked implib like this:

     $> implib /s ole32.lib c:\windows\syswow64\ole32.dll

Then I got a ole32.lib file as expected. The next step was to declare the function I want to use. For starters, I wanted to call CoInitialize(...). I defined it in D as such:

     extern( Windows ) HRESULT CoInitialize( LPVOID pvReserved );

I then called CoInitialize like this

     CoInitialize( null )

as Windows SDK docs says I should.

Finally, I try to compile everything. I have two files and ole32.lib:

     $> dmd setwallp.d ActiveDesktop.d ole32.lib

The compiler fails with the following error:

     setwallp.obj(setwallp)
      Error 42: Symbol Undefined _CoInitialize at 4
     --- errorlevel 1

Why?

I have attached a ZIP file with all my source code. As mentioned, I am trying to do something relatively simple. If I was using Phobos, I could just use CoInitialize from there (std.c.windows.com), but I am using Tango, which doesn't appear to have it defined. I could also use a third-party library like Juno, but I don't want to do that for something as simple as this should be. I could also use the ole32.lib file that comes with DMC, but it seems to correspond to an old version of ole32.dll - not that it matters much. 

Is this inherently NOT simple to do? :-) Is it at all possible to do the way I want to, or should I use one of the other approaches I outlined myself?

Hopefully, you have an answer for me. You have been very helpful alread.

Best regards,
Patrick



-------------- next part --------------
A non-text attachment was scrubbed...
Name: comexample.zip
Type: application/x-zip-compressed
Size: 13831 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20090312/fc4eb98e/attachment.bin>


More information about the Digitalmars-d-learn mailing list