Sign in to follow this  
Followers 0
Mason

Dll with Classes - How and How In AutoIt

7 posts in this topic

I created a FMOD wrapper quite some time ago, and never put it to use due to the fact that I have no intention of learning the win32 API in C++, or any slow alternative (*cough* MFC *cough*). Anyhow, this is an example of what the code looks like...(cut some of it out so it doesn't take up a few pages, if needed, I will add it back in.)

Audio.h

#ifndef _AUDIO_H
#define _AUDIO_H

#include <FMOD.h>
#include <cstring>

#define AUDIO_TYPE_MP3 101
#define AUDIO_TYPE_WAV 102
#define AUDIO_TYPE_OGG 103
#define AUDIO_TYPE_MIDI 104

class Audio
{
    public:
        Audio();
        ~Audio();
        bool LoadAudio(char* fileName);
        bool ClearAudio();
        bool PlayAudio();
        bool StopAudio();
        bool PauseAudio(bool paused);
        bool SetVolume(int volume);
    private:
        bool *isMusicLoaded;
        int *audioType;
        FSOUND_STREAM *streamHandle;
        FMUSIC_MODULE *musicHandle;
};

#endif

Audio.cpp

#include "Audio.h"

Audio::Audio()
{
    musicHandle = NULL;
    streamHandle = NULL;
    isMusicLoaded = new bool;
    *isMusicLoaded = 0;
    audioType = new int;
    FSOUND_Init (44100, 32, 0);
}

Audio::~Audio()
{
    delete isMusicLoaded;
    delete audioType;
    if(streamHandle != NULL)
        FSOUND_Stream_Close(streamHandle);
    if(musicHandle != NULL)
        FMUSIC_FreeSong(musicHandle);
    FSOUND_Close();
}

bool Audio::LoadAudio(char* fileName)
{
    if(*isMusicLoaded == 1)
        return 0;//Music Already Loaded, Don't Bother Loading More

    if(strstr(fileName, ".mp3") != NULL)
        *audioType = AUDIO_TYPE_MP3;
    else if(strstr(fileName, ".wav") != NULL)
        *audioType = AUDIO_TYPE_WAV;
    else if(strstr(fileName, ".ogg") != NULL)
        *audioType = AUDIO_TYPE_OGG;
    else if(strstr(fileName, ".mid") != NULL)
        *audioType = AUDIO_TYPE_MIDI;
    else
        return 0;//File Type Not Supported

    if(*audioType < 104)//IF AUDIO ISN'T A MIDI FILE
    {
        streamHandle = FSOUND_Stream_Open(fileName, 0, 0, 0);
        if(streamHandle == NULL)
            return 0; //error loading
        else
            *isMusicLoaded++;
    }
    else//if it is a midi file
    {
        musicHandle = FMUSIC_LoadSong(fileName);
        if(musicHandle == NULL)
            return 0;//error loading
        else
            *isMusicLoaded++;
    }

    return 1;
}

bool Audio::ClearAudio()
{
    if(streamHandle != NULL)
    {
        if(!FSOUND_Stream_Close(streamHandle))
            return 0;
    }
    else if(musicHandle != NULL)
    {
        if(!FMUSIC_FreeSong(musicHandle))
            return 0;
    }
    return 1;
}

bool Audio::PlayAudio()
{
    if(isMusicLoaded == 0)
        return 0;//No Music Loaded to be played

    if(*audioType  < 104)//If Audio isn't a midi file
    {
        if(!FSOUND_Stream_Play(0, streamHandle))
            return 0;
    }
    else
    {
        if(!FMUSIC_PlaySong(musicHandle))
            return 0;
    }
    return 1;
}

bool Audio::StopAudio()
{
    if(isMusicLoaded == 0)
        return 0;//No Music loaded to be stopped

    if(*audioType < 104)//if Audio isn't a midi file
    {
        if(!FSOUND_Stream_Stop(streamHandle));
            return 0;
    }
    else
    {
        if(!FMUSIC_StopSong(musicHandle))
            return 0;
    }
    return 1;
}

bool Audio::PauseAudio(bool paused)
{
    if(isMusicLoaded == 0)
        return 0;//No Music loaded to be paused

    if(*audioType < 104)//if Audio isn't a midi file
    {
        if(!FSOUND_SetPaused(0, paused))
            return 0;
    }
    else
    {
        if(!FMUSIC_SetPaused(musicHandle, paused))
            return 0;
    }
    return 1;
}

bool Audio::SetVolume(int volume)
{
    if(!FMUSIC_SetMasterVolume(musicHandle, volume))
        return 0;
    if(!FSOUND_SetVolume(FSOUND_ALL, volume))
        return 0;
    return 1;
}

I have minimal knowledge about DLL's...I just know how to use functions from them in my program, and I know how to inject them into a remote process. I have ran through articles on how to put this code into a DLL, but with no success. Even if I did put it into a dll, how would i use the code for my AutoIt script, or is it even possible. And if you are unclear when I say use it, I mean like so...

#include <iostream>
#include <windows.h>
#include <conio.h>

#include "Audio.h"

int main()
{
    Audio *audio = new Audio();
    audio->LoadAudio("lol.mp3");
    audio->PlayAudio();
    while (!_kbhit())
    {
    }
    audio->PauseAudio(true);
    Sleep(1000);
    audio->PauseAudio(false);
    Sleep(1000);
    audio->StopAudio();
    audio->ClearAudio();
    delete audio;
    return 0;
}

Share this post


Link to post
Share on other sites



AutoIt doesn't support using an exported class. To use this code, either create some thin wrapper functions to provide a C-like API by passing around a pointer to the structure and then having each wrapper function invoke the member function through the pointer or make the DLL only store a single global instance of the class and then each thin wrapper function would invoke the member functions through the global variable. The second approach eliminates the need to store a pointer and pass it around (Effectively the this pointer) but it only allows one instance of the class to be instantiated. The first approach means you have to store a pointer and pass it all the time and are responsible for cleaning it up. In both cases, you have to wrap the C++ class in non-member/static functions.

Share this post


Link to post
Share on other sites

I think I might consider doing htis then.

Share this post


Link to post
Share on other sites

#include "Audio.h"

#define DLLEXPORT __declspec(dllexport)

Audio *audio;

DLLEXPORT void InitMusic()
{
            Audio = new Audio();
}

DLLEXPORT  bool CreateMusic(char* path)
{
            return audio->LoadAudio(path);
}

DLLEXPORT bool PlayMusic()
{
            return audio->PlayAudio();
}

DLLEXPORT bool PauseMusic(bool yes)
{
            return audio->PauseAudio(yes);
}

DLLEXPORT bool StopMusic()
{
            return audio->Stop();
}
 
DLLEXPORT bool ClearAudio()
{
            return audio->ClearAudio();
}

DLLEXPORT bool SetVolume(int volume)
{
            return audio->SetVolume(volume);
}

I'm assuming this is what you meant? I would try and compile and use, but I am at family's on a 56k connection. I doubt it is worth it to download a IDE just for this. If you could tell me if this is the general idea that would be great...I don't want to spend the free time I have here wasting it when I could be attempting to finish some work.

Share this post


Link to post
Share on other sites

Yes, that was the basic concept.

Share this post


Link to post
Share on other sites

Why would you need an IDE? All you would need is a compiler. They tend to stay smaller than an entire editor.

Share this post


Link to post
Share on other sites

Why would you need an IDE? All you would need is a compiler. They tend to stay smaller than an entire editor.

I suppose you are right, but it isn't much use for me right now. I really can't do much without the AutoIt source code for the GUI at my house...and I could write the dll call code in autoit while I am waiting, but that takes all of 10 minutes. I go home tomorrow evening, so I will just leave it at this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0