字符串赋值不适用于C ++ DLL

时间:2015-08-05 04:14:09

标签: c# c++ wrapper

我的非托管C ++代码中的字符串赋值存在问题。没有C ++ / CLI包装器和C#GUI,一切正常。但是,字符串只是没有分配给它应该是什么。我已经创建了一个非常小的应用来证明这一点。

这些是非托管c ++应用程序的源文件和头文件:

Test.h

#pragma once
#define DllExport   __declspec( dllexport )

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <stdexcept>
#include "TestConstantsHeader.h"

namespace DataVerifier
{
    class DllExport CDataVerifier
    {
    public:
        CDataVerifier();
        ~CDataVerifier();

        //! @brief A function that processes the messages
        void Process();
    };
}

TestConstantsHeader.h

#pragma once
#define DllExport   __declspec( dllexport )

#include <iostream>

namespace DataVerifier
{
    namespace Structure
    {
        const std::string strHTMLTemplate = "<doctype html>"
            "<html lang=\"en\">"
            "<head>"
            "<meta charset=\"utf-8\"/>"
            "<title>Data Verifier Test Results - {@testdesc}</title>"
            "<style>"
            "body {"
            "\tbackground: none repeat scroll 0 0 #F3F3F4;"
            "\tcolor: #1E1E1F;"
            "\tfont-family: \"Segoe UI\",Tahoma,Geneva,Verdana,sans-serif;"
            "\tmargin: 0;"
            "\tpadding: 0;"
            "}"
            "h1 {"
            "\tbackground-color: #E2E2E2;"
            "\tborder-bottom: 1px solid #C1C1C2;"
            "\tcolor: #201F20;"
            "\tfont-size: 21pt;"
            "\tfont-weight: normal;"
            "\tmargin: 0;"
            "\tpadding: 10px 0 10px 10px;"
            "}"
            "h2 {"
            "\tfont-size: 18pt;"
            "\tfont-weight: normal;"
            "\tmargin: 0;"
            "\tpadding: 15px 0 5px;"
            "}"
            "h3 {"
            "\tbackground-color: rgba(0, 0, 0, 0);"
            "\tfont-size: 15pt;"
            "\tfont-weight: normal;"
            "\tmargin: 0;"
            "\tpadding: 15px 0 5px;"
            "}"
            "a {"
            "\tcolor: #1382CE;"
            "}"
            "table {"
            "\tborder-collapse: collapse;"
            "\tborder-spacing: 0;"
            "\tfont-size: 10pt;"
            "}"
            "table th {"
            "\tbackground: none repeat scroll 0 0 #E7E7E8;"
            "\tfont-weight: normal;"
            "\tpadding: 3px 6px;"
            "\ttext-align: left;"
            "\ttext-decoration: none;"
            "}"
            "table td {"
            "\tbackground: none repeat scroll 0 0 #F7F7F8;"
            "\tborder: 1px solid #E7E7E8;"
            "\tmargin: 0;"
            "\tpadding: 3px 6px 5px 5px;"
            "\tvertical-align: top;"
            "}"
            ""
            ".textCentered {"
            "\ttext-align: center;"
            "}"
            ".messageCell {"
            "\twidth: 100;"
            "}"
            "#content {"
            "\tpadding: 0 12px 12px;"
            "}"
            "#overview table {"
            "\tmax-width: 75;"
            "\twidth: auto;"
            "}"
            "#messages table {"
            "\twidth: 97;"
            "}"
            "</style>"
            "</head>"
            "<body>"
            "<div id=\"big_wrapper\">"
            "\t<h1>Test Results - {@testdesc}</h1>"
            "\t<table>"
            "{@eeddata}"
            "\t</table>"
            "</body>"
            "</html>";
    }
}

Test.cpp的

#include "Test.h"

using namespace DataVerifier;

CDataVerifier::CDataVerifier()
{
}

CDataVerifier::~CDataVerifier(){}

void CDataVerifier::Process()
{
    std::string strTmp = Structure::strHTMLTemplate;
    std::cout << strTmp;
}

我的TestCLR.h类库(CLR)

// TestCLR.h

#pragma once
#include <stdexcept>

#include "../Test1/Test.h"
#include "../Test1/TestConstantsHeader.h"

using namespace System;
using namespace DataVerifier;

namespace TestCLR {

    public ref class Class1
    {
    public:
        Class1();
        ~Class1();
        // TODO: Add your methods for this class here.
        void CheckStringAssignment();

        CDataVerifier *dv;
    };
}

TestCLR.cpp

// This is the main DLL file.

#include "stdafx.h"

#include "TestCLR.h"

TestCLR::Class1::Class1()
{
    dv = new CDataVerifier();
}

TestCLR::Class1::~Class1()
{}

void TestCLR::Class1::CheckStringAssignment()
{
    dv->Process();
}

Main.cpp(在非托管C ++应用程序中)

#include <iostream>

#include "Test.h"

int main()
{
    return 0;
}

最后,C#GUI代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TestCLR;

namespace Test1GUI
{
    public partial class Form1 : Form
    {
        Class1 cs = new Class1();
        public Form1()
        {
            InitializeComponent();
            cs.CheckStringAssignment();
        }
    }
}

如果我进入CheckStringAssignment然后进入Process(非托管代码),接下来是分配发生的行,字符串保持为空。

知道可能会发生什么吗?

由于

0 个答案:

没有答案