【问题标题】:How do I access functions from libsndfile-1.dll in MSVC?如何从 MSVC 中的 libsndfile-1.dll 访问函数?
【发布时间】:2009-10-05 23:55:52
【问题描述】:

  我无法让 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

有人对我哪里出错有任何建议吗?

非常感谢,
乔什。

【问题讨论】:

    标签: c visual-c++ dll libsndfile


    【解决方案1】:

    嗯,我真的应该学会不要在深夜发帖! 今天早上我又尝试了一次,并在几分钟内打开了文件。 我的路径都错了(不习惯这些奇怪的 Windows 路径)! 我尝试使用相对路径和宾果游戏! 希望对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多