【问题标题】:Linking between c# and c++ in 64 bit machine在 64 位机器中 c# 和 c++ 之间的链接
【发布时间】:2016-10-03 00:44:12
【问题描述】:

我已经用 c++ 和 c# 编写了代码。从我的 c++ 代码中,我通过调用我的 c# 函数。我只发送了一部分 C++ 代码。

txtPath 包含文本文件的位置。 C++代码:

    CoInitialize(NULL);
    IMyClassPtr obj3;
    obj3.CreateInstance(__uuidof(Program));
    obj3->Validation(txtPath);
    CoUninitialize();

Validation() 是我的 c# 函数。 我的 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ABC
{
    [ComVisible(true)]
    public interface IMyClass
    {
        void Validation(string txtp);

    }


    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]

    public class Program : IMyClass
    {


        private string replace_string(string text)
        {
            return text.Replace("\r\a", "");
        }

        public void Validation(string txtp)
        {
            string[] textValidate = File.ReadAllLines(txtp);
            string textpath = txtp;
            //validation starts here
            foreach (string line in textValidate)
            {
                string[] strsplit = line.Split(new string[] { "," }, StringSplitOptions.None);
                string task = strsplit[0];
                string sign = strsplit[1];
                string person = strsplit[2];
                string routing = strsplit[3];

                if (String.IsNullOrEmpty(task) || String.IsNullOrEmpty(sign) || String.IsNullOrEmpty(person))
                {
                    //if the txt file is invalid
                    MessageBox.Show("Signature.txt is incomplete or has invalid input!!!");

                }


            }

        }
}
}

我已经在 c# 中完成了所有必需的设置。C# settings snapshot C# 项目是一个类库。我的代码在 32 位机器上运行良好。我通过 regasm.exe 注册它在其他系统中使用生成的 tlb。

c++ 代码在 64 位机器上工作,但是当 c# 链接代码被命中时,执行停止而没有抛出任何错误。我正在使用 64 位机器并使用相同的代码创建了一个新项目。请帮忙

【问题讨论】:

    标签: c# c++ com-interop regasm tlb


    【解决方案1】:

    让您为您的目标平台使用正确版本的“regasm.exe”(即“C:\Windows\Microsoft.NET\Framework64\v2.0.50727\RegAsm.exe”)。记下“Framework64”。

    【讨论】:

      【解决方案2】:

      在 64 位 Windows 上,Microsoft 不支持将 64 位 DLL 加载到 32 位进程中,反之亦然。有关更多信息,请参阅 MSDN 上的以下资源:

      http://msdn.microsoft.com/en-us/library/aa384231(VS.85).aspx

      【讨论】:

      • 好的,但我现在使用的是 64 位机器。我用相同的代码在 64 位机器上创建了一个新项目。我的代码不起作用@JacobSeleznev
      • 链接的文章谈到了 64 位 Windows 上的 32 位进程。
      猜你喜欢
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      相关资源
      最近更新 更多