Advertisement Jump to content


This topic is now archived and is closed to further replies.


is it possible to mix C and C++ ?

This topic is 6115 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a Fs2002 gauge project written enterely in C and I need to include Directsound capacity in it. Can I call C++ function (from DX''s playsound sample ) by the C''s gauge project ? if yes how ? (CINTERFACE ?? ) I have already do some test but the compiler don''t stop to complaint abnout things so I wonder if this is the right direction. I grabbed old DirectSound C code showing how to play wave perhaps it will be more simple to go with it ? Thanks for any input Dan

Share this post

Link to post
Share on other sites
DirectSound is based on COM so you should be able to interface with it using C. The classes in dsutil.h and dsutil.cpp appear simple enough to write in C or to extract the relevant pieces. If the C++ code is compiled using ''extern "C"'' you should be able to interface with it too, otherwise, you might have troubles with name mangling.

Share this post

Link to post
Share on other sites
every COM interface can be used in C.

In C++, you could write something like

IDirectSound* blah;

but in C, you would have to call something like

IDirectSound* blah;

I know for a fact that in DX they use macros for C so that you can call something like this:

IDirectSound* blah;

Check the DirectX docs on using DX in C, I know for a fact that its there.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
You can suppress name mangling in C++ by making your function prototypes like this:

extern "C" int f();

You can then call C++ functions from C code.

Share this post

Link to post
Share on other sites
Here's the straight C implementation of the IUnknown interface - with out qualifying all the variables - take a look a dsound.h for the same kind of construct. DSound uses a different intializer function than CoInitialize etc.

#ifndef __IUnknown_INTERFACE_DEFINED__
#define __IUnknown_INTERFACE_DEFINED__
typedef IUnknown *LPUNKNOWN;
extern const IID IID_IUnknown;
typedef struct IUnknownVtbl {
HRESULT (STDMETHODCALLTYPE *QueryInterface)(IUnknown *,REFIID,void * *);
} IUnknownVtbl;
interface IUnknown { CONST_VTBL struct IUnknownVtbl *lpVtbl; };
#define IUnknown_QueryInterface(T,r,O) (T)->lpVtbl->QueryInterface(T,r,O)
#define IUnknown_AddRef(T) (T)->lpVtbl->AddRef(T)
#define IUnknown_Release(T) (T)->lpVtbl->Release(T)
HRESULT _stdcall IUnknown_QueryInterface_Proxy(IUnknown * T,REFIID,void * *);
void __RPC_STUB IUnknown_QueryInterface_Stub(IRpcStubBuffer *, IRpcChannelBuffer *, PRPC_MESSAGE, DWORD *);
ULONG _stdcall IUnknown_AddRef_Proxy(IUnknown *);
void __RPC_STUB IUnknown_AddRef_Stub(IRpcStubBuffer *,IRpcChannelBuffer *,PRPC_MESSAGE, DWORD *);
ULONG _stdcall IUnknown_Release_Proxy(IUnknown * );
void __RPC_STUB IUnknown_Release_Stub(IRpcStubBuffer *,IRpcChannelBuffer *,PRPC_MESSAGE,DWORD *);

[edited by - lessbread on April 20, 2002 5:04:46 PM]

Share this post

Link to post
Share on other sites
if you use a c++ compiler, then mixing the code i much easier since you can use the c++ code and c code without having to convert c++ stuff to the c equvilient meaning easier time using the majority of samples avaible. however, if you cannot do this, then you are stuck learning how to use c++ constructs in c, then converting the tutorials and samples yourself which is not too hard (because after all tutorials are designed to help you understand the concept not give you code to copy). you can look into asm dx tutorials since they need to access the dx objects like you do in c.

btw just an fyi, nearly all c code can be compiled using a c++ compiler without many changes if any.

Share this post

Link to post
Share on other sites
During the last hour I implemented DS into
my gauge and it work perfectly.

Effectively I used things like: DirectSoundBuffer8_Lock()
etc etc...

But all your info will be useful for my incoming work.

Thanks all,

"nothing more to learn ? time to die!"

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!