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

发布于 2024-08-07 00:06:17 字数 2158 浏览 3 评论 0原文

  我在让 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 时,输出为:
版本:libsndfile-1.0.17
哦!无法打开 SNDFILE

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

非常感谢,
乔什。

  I'm having trouble getting libsndfile-1.dll to work in my MSVC project. I can load the library and retrieve the version string from the dll by calling sf_command() from my code. However, I can't seem to get sf__open() to return a SNDFILE pointer.

I've also noticed that I can't get fopen() to return a FILE pointer either (maybe this is related, I think sf_open() uses fopen()!?).

I'm pretty new to MSVC, C/C++ and windows in general so I'm probably missing something really obvious.

My main.cpp looks like this:

#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;
}

The project builds and exits with code 3 (couldn't open the file! (I'm pretty sure the file is there!!)).

When I run the exe the output is:
Version: libsndfile-1.0.17
Doh! couldn't open the SNDFILE

Does anyone have any suggestions as to where I'm going wrong?

Many thanks,
Josh.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2024-08-14 00:06:17

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

Hmm, I really should learn not to post to forums late at night!
I had another attempt this morning and had the file open within minutes.
I was getting my paths all wrong (not used to these weird windows paths)!
I tried using a relative path and bingo!
Hope that helps someone!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文