简体   繁体   English

移植到MSVS2010 C ++ GUI的MSVS2010 C ++控制台代码失败。 为什么?

[英]MSVS2010 C++ Console Code Ported to MSVS2010 C++ GUI is Failing. Why?

I just completed a proof of concept, or so I thought, of feeding Microsoft Visual Studio 2010 some C++ code as a console program. 我刚刚完成了一个概念证明,或者说是我想的,是向Microsoft Visual Studio 2010提供一些C ++代码作为控制台程序。 The C++ code that compiled is given below: 下面给出了编译的C ++代码:

#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
#include <sndfile.h>


//The following libraries are related to parsing the text files
#include <iostream> //Open the file 
#include <fstream> //Reading to and from files



//The following libraries are for conducting the DTW analysis
#include "dtw.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    printf("This is a test\n");


    //This will be the length of the buffer used to hold samples while the program processes them. 


    //A SNDFILE is like FILE in a standard C library. Consequently, the sf_open_read and sf_open_write functions will return an 
    //SNDFILE* pointer when they successfully open the specified file. 
    SNDFILE* sf = NULL; 

    /*SF_INFO will obtain information of the file we wish to load into our program. */
    SF_INFO info; 

    /*The following is descriptive information to obtain from wave files. These are declarations*/
    int num_channels;
    double num, num_items;S
    double *buf; 
    int f, sr, c; 
    int i,j;
    FILE *out; 


    /*This is where the program will open the WAV file */
    info.format = 0; 
    sf = sf_open("C:\\Users\\GeekyOmega\\Desktop\\gameon.wav", SFM_READ, &info);
    if(sf == NULL)
    {
        printf("Failed to open the file.\n");
        getchar();
        exit(-1);
    }

    /*Print some file information */
    f = info.frames;
    sr = info.samplerate;
    c = info.channels;

    /*Print information related to file*/
    printf("frames = %d\n",f);
    printf("sample rate = %d\n",sr);
    printf("channels = %d\n",c);

    /*Calculate and print the number of items*/  
    num_items = f*c;
    printf("Read %lf items\n", num_items);

    /*Allocate space for the data to be read*/
    buf = (double *) malloc(num_items*sizeof(double));
    num = sf_read_double(sf,buf,num_items);
    sf_close(sf);

    /*print the information*/
    printf("Read %lf items\n", num);



    /*Write the data to the filedata.out*/  
    out = fopen("filedata.txt", "w");
    for(i = 0; i < num; i+=c)
    {
        for(j = 0; j < c; ++j)
        {
            fprintf(out, "%lf ", buf[i +j]);
        }
        fprintf(out,"\n");
    }
    fclose(out);

} }

So next, and this is critical, I want this to work with a GUI. 因此,接下来,这很关键,我希望它可以与GUI一起使用。 That is, I load in any file I want and it converts that wav file to text. 也就是说,我加载了我想要的任何文件,它将wav文件转换为文本。 I provide that code below: 我在下面提供该代码:

#pragma once
//Libraries required for libsndfile
#include <sndfile.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>


namespace WaveGui {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    using namespace System::Runtime::InteropServices;



    using namespace std;

    /// <summary>
    /// Summary for Form1
    /// </summary>

I edited this for readability. 为了便于阅读,我对此进行了编辑。 It was a standard MSVS2010 form. 这是标准的MSVS2010表格。 I only added a button and open file dialog. 我只添加了一个按钮并打开了文件对话框。

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
             {

                if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
                 {  
                    //Note: Ask Gustafson how we might free the memory for this strign
                    //  http://support.microsoft.com/?id=311259

                    char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(openFileDialog1->FileName);
                    SNDFILE* sf = NULL; 
                    SF_INFO info;

                    /*The following is descriptive information to obtain from wave files. These are declarations*/
                    int num_channels;
                    double num, num_items;
                    double *buf; 
                    int f, sr, c; 
                    int i,j;
                    FILE *out; 


                    /*This is where the program will open the WAV file */
                    info.format = 0; 
                    sf = sf_open(str2, SFM_READ, &info);
                    if(sf == NULL)
                    {
                        exit(-1);
                    }

                    /*Print some file information */
                    f = info.frames;
                    sr = info.samplerate;
                    c = info.channels;

                    /*Calculate and print the number of items*/  
                    num_items = f*c;

                    /*Allocate space for the data to be read*/
                    buf = (double *) malloc(num_items*sizeof(double));
                    num = sf_read_double(sf,buf,num_items);
                    sf_close(sf);

                    /*Write the data to the filedata.out*/  
                    out = fopen("filedata.txt", "w");
                    for(i = 0; i < num; i+=c)
                    {
                        for(j = 0; j < c; ++j)
                        {
                            fprintf(out, "%lf ", buf[i +j]);
                        }
                        fprintf(out,"\n");
                    }
                    fclose(out);



                 }




             }
    };
}

In the code for the button, I convert from a system string to regular string and then try to use the C++ code above to convert my wave file to txt information. 在按钮的代码中,我从系统字符串转换为常规字符串,然后尝试使用上面的C ++代码将wave文件转换为txt信息。 I know the conversion code works, and I know the button code works as I have tested them separately. 我知道转换代码有效,并且我知道按钮代码有效,因为我已经分别测试了它们。 However, my library sndfile.h really dies when I try to use it now. 但是,当我现在尝试使用它时,我的库sndfile.h确实死了。

The error it gives me when I added the libraries stdio.h, Windows.H, stdlib.h, iostream, fstream is as follows: 添加库stdio.h,Windows.H,stdlib.h,iostream,fstream时出现的错误如下:

1>WaveGui.obj : error LNK2031: unable to generate p/invoke for "extern "C" int __clrcall sf_close(struct SNDFILE_tag *)" (?sf_close@@$$J0YMHPAUSNDFILE_tag@@@Z); calling convention missing in metadata
1>WaveGui.obj : error LNK2031: unable to generate p/invoke for "extern "C" __int64 __clrcall sf_read_double(struct SNDFILE_tag *,double *,__int64)" (?sf_read_double@@$$J0YM_JPAUSNDFILE_tag@@PAN_J@Z); calling convention missing in metadata
1>WaveGui.obj : error LNK2031: unable to generate p/invoke for "extern "C" struct SNDFILE_tag * __clrcall sf_open(char const *,int,struct SF_INFO *)" (?sf_open@@$$J0YMPAUSNDFILE_tag@@PBDHPAUSF_INFO@@@Z); calling convention missing in metadata
1>WaveGui.obj : warning LNK4248: unresolved typeref token (01000027) for 'SNDFILE_tag'; image may not run
1>WaveGui.obj : error LNK2028: unresolved token (0A000022) "extern "C" int __clrcall sf_close(struct SNDFILE_tag *)" (?sf_close@@$$J0YMHPAUSNDFILE_tag@@@Z) referenced in function "private: void __clrcall WaveGui::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@WaveGui@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>WaveGui.obj : error LNK2028: unresolved token (0A000023) "extern "C" __int64 __clrcall sf_read_double(struct SNDFILE_tag *,double *,__int64)" (?sf_read_double@@$$J0YM_JPAUSNDFILE_tag@@PAN_J@Z) referenced in function "private: void __clrcall WaveGui::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@WaveGui@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>WaveGui.obj : error LNK2028: unresolved token (0A000025) "extern "C" struct SNDFILE_tag * __clrcall sf_open(char const *,int,struct SF_INFO *)" (?sf_open@@$$J0YMPAUSNDFILE_tag@@PBDHPAUSF_INFO@@@Z) referenced in function "private: void __clrcall WaveGui::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@WaveGui@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>WaveGui.obj : error LNK2019: unresolved external symbol "extern "C" int __clrcall sf_close(struct SNDFILE_tag *)" (?sf_close@@$$J0YMHPAUSNDFILE_tag@@@Z) referenced in function "private: void __clrcall WaveGui::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@WaveGui@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>WaveGui.obj : error LNK2019: unresolved external symbol "extern "C" __int64 __clrcall sf_read_double(struct SNDFILE_tag *,double *,__int64)" (?sf_read_double@@$$J0YM_JPAUSNDFILE_tag@@PAN_J@Z) referenced in function "private: void __clrcall WaveGui::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@WaveGui@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>WaveGui.obj : error LNK2019: unresolved external symbol "extern "C" struct SNDFILE_tag * __clrcall sf_open(char const *,int,struct SF_INFO *)" (?sf_open@@$$J0YMPAUSNDFILE_tag@@PBDHPAUSF_INFO@@@Z) referenced in function "private: void __clrcall WaveGui::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@WaveGui@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>c:\users\geekyomega\documents\visual studio 2010\Projects\WaveGui\Debug\WaveGui.exe : fatal error LNK1120: 6 unresolved externals

As near as I can tell, I installed the library right. 据我所知,我已正确安装了磁带库。 After all, it works perfectly fine with these libraries in a console situation. 毕竟,在控制台情况下,这些库可以很好地工作。 However, when I try to use the exact same code and libraries with my GUI form, it seems that the libraries are not playing nice with each other and as a result, I can't read my .h file right and access structs like SNDFILE. 但是,当我尝试在GUI表单中使用完全相同的代码和库时,似乎库之间无法很好地协作,结果,我无法正确读取.h文件并访问SNDFILE之类的结构。

Can someone please let me know what is going wrong? 有人可以让我知道怎么了吗? I have spent hours on this and I hope I don't have to scrap the libsndfile library. 我已经花了几个小时在此上,我希望不必废弃libsndfile库。 I really want to get it to work with MSVS2010, with the GUI and as far as I can tell, there is no reason this shouldn't be working. 我真的很想让它与MSVS2010,GUI一起使用,据我所知,没有理由不应该这样做。 But you know what they say, computers don't lie. 但是您知道他们说的是什么,计算机不会说谎。

As always, thanks for your patient help. 与往常一样,感谢您的耐心帮助。 GeekyOmega GeekyOmega

So, your console app is native code, but your GUI app is managed C++. 因此,您的控制台应用程序是本机代码,而您的GUI应用程序是托管C ++。 Was that intentional, or did you intend for your GUI app to be native (Win32) code too? 这是故意的,还是您也打算将GUI应用程序作为本机(Win32)代码?

I think you'll have better luck if you either go with 100% native code, or perhaps create a DLL to encapsulate your native code and call that via P/Invoke from your .NET GUI. 我认为,如果您使用100%本机代码,或者创建一个DLL来封装本机代码并通过.NET GUI中的P / Invoke调用它,将会更好。 Here's an example of using P/Invoke: 这是使用P / Invoke的示例:

http://manski.net/2012/05/29/pinvoke-tutorial-basics-part-1/ http://manski.net/2012/05/29/pinvoke-tutorial-basics-part-1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从MSVS2005移植C ++ .net代码到MSVS2010时找不到资源(?) - Resource not found(?) while porting C++ .net code from MSVS2005 to MSVS2010 为什么我会在使用其他库时遇到 LNK4098 冲突 - 尝试在 MSVS2010 Express 中编译 C++ 时? - Why do I get LNK4098 conflicts with use of other libs - when trying to compile C++ in MSVS2010 Express? 在msvs2010上使用curl编译项目 - compiling project using curl on msvs2010 MSVS2010中“%”宏与“$”宏之间的差异 - Difference between “%” macros and “$” macros in MSVS2010 在MSVS2010中使用静态链接库(MSVS2008) - usage of static linked library (MSVS2008) in MSVS2010 MSVS2010链接器错误悲伤-不能完全确定出什么问题了 - MSVS2010 linker error sadness - not entirely sure what is wrong xcode 4中的赋值运算符崩溃在MSVS2010中可以正常运行 - Assignment operator crash in xcode 4, runs fine in MSVS2010 使用MSVS 2010和C ++标准构建问题 - Build issue with MSVS 2010 and the C++ standard 从MSVS2005到MSVS2010的端口:我再也找不到ReadProcessorPwrScheme() - Port from MSVS2005 to MSVS2010: I canno longer find ReadProcessorPwrScheme() 使用dllimport收到的空指针[MSVS C ++ 2010] - Using void pointer received by dllimport [MSVS C++ 2010]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM