Jump to content
  • Advertisement

Archived

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

dansteph

is it possible to mix C and C++ ?

This topic is 6021 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
Advertisement
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;
blah->SomeFunction();

but in C, you would have to call something like

IDirectSound* blah;
blah->vtable->SomeFunction(&blah);

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

IDirectSound* blah;
blah->IDirectSound_SomeFunction();

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 {
BEGIN_INTERFACE
HRESULT (STDMETHODCALLTYPE *QueryInterface)(IUnknown *,REFIID,void * *);
ULONG (STDMETHODCALLTYPE *AddRef)(IUnknown * );
ULONG (STDMETHODCALLTYPE *Release)(IUnknown *);
END_INTERFACE
} IUnknownVtbl;
interface IUnknown { CONST_VTBL struct IUnknownVtbl *lpVtbl; };
#ifdef COBJMACROS
#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)
#endif
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 *);
#endif



[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,

Dan
"nothing more to learn ? time to die!"

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!