简体   繁体   English

在设置中嵌入C ++本机dll

[英]embedding c++ native dll in set up

I want to embed c++ native dll in set up file created with install shield limited edition. 我想将c ++本机dll嵌入使用install shield限量版创建的设置文件中。

Hint :- My application created by using c# and c++ native dll. 提示:-我的应用程序是使用c#和c ++本机dll创建的。

Here is my example :- 这是我的例子:

My c++ dll_code 我的C ++ dll_code

extern "c" __declspec(dllexport) int function_c ()
{
    int a=10;
    return a;
}

My .net code 我的.net代码

public partial class Form1 : Form
{
    [DllImport(@"C:\Users\bajwa\Documents\Visual Studio 2012\Projects\c++dll\c++_dll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
    static extern int function_c();

    void csharp_function()
    {
        int result= function_c(); // calling c++ native  function
        MessageBox.Show(result);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        csharp_function(); // calling c# function.
    }
}

When I installed this setup on my computer it runs perfectly. 当我在计算机上安装此设置时,它可以完美运行。 Because C++ native dll is placed on my computer at "C:\\Users\\bajwa\\Documents\\Visual Studio 2012\\Projects\\c++dll\\c++_dll.dll". 因为C ++本机dll位于我的计算机上,所以位于“ C:\\ Users \\ bajwa \\ Documents \\ Visual Studio 2012 \\ Projects \\ c ++ dll \\ c ++ _ dll.dll”。

But when I delete the c++ native dll from that location then it shows the error. 但是,当我从该位置删除c ++本机dll时,它将显示错误。

dll not fount at this location dll不在此位置

Please help and solve my problem. 请帮助解决我的问题。

I think you will just need to use a relative path for the imported DLL. 我认为您只需要为导入的DLL使用相对路径。 The easiest thing to do would be to copy the DLL to a known folder relative to the executable (maybe even the same folder) and then use that path. 最简单的方法是将DLL复制到相对于可执行文件的已知文件夹(甚至是同一文件夹),然后使用该路径。 So if they share a folder, your code can become this: 因此,如果他们共享一个文件夹,则您的代码可以如下所示:

 [DllImport("c++_dll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern int function_c();

Then just modify the installer to place the dll in the correct location. 然后只需修改安装程序以将dll放置在正确的位置。 To test, just change your code and move the dll over, but it should work. 要进行测试,只需更改您的代码并将dll移到其他位置,但这应该可以工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM