On Friday, 27 May 2016 at 16:08:27 UTC, Kagamin wrote:
On Friday, 27 May 2016 at 15:28:42 UTC, Andrew Edwards wrote:
Have you tried with extern(C) yet?
extern(C) is for undecorated symbold
extern(Windows) adds the _ and @12 decorations (would be __stdcall on C/C++ side)

The thought never crossed my mind. Tried it and it works like a charm. Thanks to everyone who responded.

If you declare extern function with wrong calling convention, calling it can result in stack corruption.

Okay, that makes sense. Will remain mindful of that.

Once DirectSoundCreate() returns successfully, I need to call one of the function pointers in the struct but am at a loss of how to define it based on the interface presented.

This struct is called COM interface, D has built-in support for them: https://dlang.org/spec/interface.html#com-interfaces

So if I'm understanding correctly, I should be able to take this:

    DECLARE_INTERFACE_(IDirectSound, IUnknown)
    {
        // IUnknown methods
STDMETHOD(QueryInterface) (THIS_ REFIID, LPVOID FAR *) PURE;
        STDMETHOD_(ULONG,AddRef)        (THIS) PURE;
        STDMETHOD_(ULONG,Release)       (THIS) PURE;

        // IDirectSound methods
STDMETHOD(CreateSoundBuffer) (THIS_ LPCDSBUFFERDESC, LPDIRECTSOUNDBUFFER *, LPUNKNOWN) PURE;
        STDMETHOD(GetCaps)              (THIS_ LPDSCAPS) PURE;
STDMETHOD(DuplicateSoundBuffer) (THIS_ LPDIRECTSOUNDBUFFER, LPDIRECTSOUNDBUFFER *) PURE;
        STDMETHOD(SetCooperativeLevel)  (THIS_ HWND, DWORD) PURE;
        STDMETHOD(Compact)              (THIS) PURE;
        STDMETHOD(GetSpeakerConfig)     (THIS_ LPDWORD) PURE;
        STDMETHOD(SetSpeakerConfig)     (THIS_ DWORD) PURE;
        STDMETHOD(Initialize)           (THIS_ LPGUID) PURE;
    };

Convert it into this:

    extern (C) class IDirectSound : IUnknown
    {
        // IUnknown methods
        HRESULT QueryInterface(const(IID)*, void**);
        uint AddRef();
        uint Release();

        // IDirectSound methods
HRESULT CreateSoundBuffer(LPCDSBUFFERDESC, LPDIRECTSOUNDBUFFER*, LPUNKNOWN);
        HRESULT GetCaps(LPDSCAPS);
HRESULT DuplicateSoundBuffer(LPDIRECTSOUNDBUFFER, LPDIRECTSOUNDBUFFER*);
        HRESULT SetCooperativeLevel(HWND, DWORD);
        HRESULT Compact();
        HRESULT GetSpeakerConfig(LPDWORD);
        HRESULT SetSpeakerConfig(DWORD);
        HRESULT Initialize(LPGUID);
    }

Import the correct libraries:

    import core.sys.windows.windows;
    import core.sys.windows.com;

And finally link to the lib file and I should be golden!!!

Except I'm not :(

C:\Users\edwarac\work\sound\code>build
A subdirectory or file ..\..\build already exists.
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
sound.obj(sound)
Error 42: Symbol Undefined __D8sound12IDirectSound17CreateÇàìBufferMWxPSÇï»DSBUFFERDESCPPCÇèÜ8Çî╔Çå╝C4core3sys7windows6unknwn8IU·ownZi@12
sound.obj(sound)
Error 42: Symbol Undefined __D8sound12IDirectSound16SetSpeakerConfigMWkZi@4
sound.obj(sound)
Error 42: Symbol Undefined __D8sound12IDirectSound16GetSpeakerConfigMWPkZi@4
sound.obj(sound)
 Error 42: Symbol Undefined __D8sound12IDirectSound7CompactMWZi@0
sound.obj(sound)
Error 42: Symbol Undefined __D8sound12IDirectSound20DuplicateSoundBufferMWPC8sound18IDirectSoundBufferPPC8sound18IDirectSoundBufferZi@8
sound.obj(sound)
Error 42: Symbol Undefined __D8sound12IDirectSound19SetCooperativeLevelMWPvkZi@8
sound.obj(sound)
Error 42: Symbol Undefined __D8sound12IDirectSound10InitializeMWPS4core3sys7windows8basetyps4GUIDZi@4
sound.obj(sound)
Error 42: Symbol Undefined __D8sound12IDirectSound7GetCapsMWPS8sound6DSCAPSZi@4
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer12GetFrequencyMWPkZi@4
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer6SetPanMWiZi@4
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer9GetFormatMWPS4core3sys7windows8mmsystem12WAVEFORMATEXkPkZi@12
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer7RestoreMWZi@0
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer6GetPanMWPiZi@4
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer4StopMWZi@0
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer9GetStatusMWPkZi@4
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer4LockMWkkPPvPkPPvPkkZi@28
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer7GetCapsMWPS8sound7DSBCAPSZi@4
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer12SetFrequencyMWkZi@4
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer9SetFormatMWPxS4core3sys7windows8mmsystem12WAVEFORMATEXZi@4
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer9SetVolumeMWiZi@4
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer18SetCurrentPositionMWkZi@4
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer9GetVolumeMWPiZi@4
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer18GetCurrentPositionMWPkPkZi@8
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer6UnlockMWPvkPvkZi@16
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer4PlayMWkkkZi@12
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer10InitializeMWPC8sound12IDirectSoundxPS8sound12DSBUFFERDESCZi@8
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer7ReleaseMWZk@0
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer6AddRefMWZk@0
sound.obj(sound)
Error 42: Symbol Undefined __D8sound12IDirectSound14QueryInterfaceMWPxS4core3sys7windows8basetyps4GUIDPPvZi@8
sound.obj(sound)
 Error 42: Symbol Undefined __D8sound12IDirectSound7ReleaseMWZk@0
sound.obj(sound)
 Error 42: Symbol Undefined __D8sound12IDirectSound6AddRefMWZk@0
sound.obj(sound)
Error 42: Symbol Undefined __D8sound18IDirectSoundBuffer14QueryInterfaceMWPxS4core3sys7windows8basetyps4GUIDPPvZi@8
--- errorlevel 32


Reply via email to