如何从MSVC中的libsndfile-1.dll访问函数?

时间:2009-10-05 23:55:52

标签: c visual-c++ dll libsndfile

我无法让libsndfile-1.dll在我的MSVC项目中工作。我可以通过从我的代码中调用sf_command()来加载库并从dll中检索版本字符串。但是,我似乎无法让sf__open()返回SNDFILE指针。

我也注意到我无法让fopen()返回FILE指针(也许这是相关的,我认为sf_open()使用fopen()!?)。

我对MSVC,C / C ++和Windows一般都很陌生,所以我可能会遗漏一些非常明显的东西。

我的main.cpp看起来像这样:

#include <windows.h>
#include <stdio.h>

#include "sndfile.hh"

// create some function pointers to point to the dll function addresses
// I'm winging this a bit. hopefully it's right!? seems to work!
typedef int (*SF_COMMAND)(SNDFILE*, int, void*, int); 
typedef SNDFILE* (*SF_OPEN)(const char*, int, SF_INFO*); 

int main()
{
    // dll handle
    HINSTANCE hDLL = NULL;

    // create some vars to store the dll funcs in
    SF_COMMAND     sf_command;
    SF_OPEN        sf_open;

    // load the dll
    hDLL = LoadLibrary(L"libsndfile-1.dll");

    // check the dll loaded
    if( NULL == hDLL )
    {
        printf("Error, Could not load library \n");
        return 1;
    }

    // get the dll funcs
    sf_command = (SF_COMMAND)GetProcAddress(hDLL, "sf_command");
    sf_open = (SF_OPEN)GetProcAddress(hDLL, "sf_open");

    // check we got the funcs
    if(!(sf_command && sf_open)){
        printf("Error exporting dll functions \n");
        return 2;
    }

    // all good so far!
    // try the first function
    char* version_string[sizeof(char*)*4];
    int res = sf_command(NULL, SFC_GET_LIB_VERSION, &version_string, sizeof(version_string));

    if(res){
        // all good!
        printf("Version: %s \n", version_string);
    }

    // now try and create a SNDFILE pointer
    SF_INFO info;
    SNDFILE* sfp = sf_open("c:\\Godspeed.aif", SFM_READ, &info);

    if(sfp){
        printf("Hurray! successfully opened the SNDFILE!! \n");
    }else{
        printf("Doh! couldn't open the SNDFILE!! \n");
        // Grr!!
        return 3;
    }

    return 0;
}

项目使用代码3构建和退出(无法打开文件!(我很确定文件在那里!!)。)

当我运行exe时,输出为:
Version: libsndfile-1.0.17
Doh! couldn't open the SNDFILE

有没有人对我出错的地方有任何建议?

非常感谢,
约什

1 个答案:

答案 0 :(得分:0)

嗯,我真的应该学会不要在深夜发布到论坛! 我今天早上再次尝试,并在几分钟内打开文件。 我的路径都错了(不习惯这些奇怪的窗口路径)! 我尝试使用相对路径和宾果游戏! 希望能帮助别人!

相关问题