c# - 从外部函数 C++ DLL 调用静态函数

标签 c# c++ dllimport extern

我编写了一个 C++ DLL,其中包含 2 个外部“C”函数以及一个从其中一个外部函数中调用的静态函数。当我尝试从 C# 调用外部函数时,调用静态函数的函数(也包含在 DLL 中)我收到构建警告(“方法、运算符或访问器‘...’被标记为外部并且在它。考虑添加一个 DllImport 属性来指定外部实现。)。当我注释掉静态函数调用时,一切都顺利编译。关于如何解决这个问题的任何想法?我是否也必须在静态函数上使用 extern?

我的代码如下:

#include <stdio.h>
#include <sqlite3.h>
#include <iostream>
#include <ios>
#include <string>
#include <vector>

using std::string;
using std::vector;

// PARSE CSV FILE
static void ParseCSV(const string& csvSource, vector<vector<string> >& lines)
{
        // static function to parse CSV
}    

extern "C"
{


    // somefunction([MarshalAs(UnmanagedType.LPStr) string text) [c#]
    __declspec(dllexport) bool SQLite_ImportCSV(const char *dbPath, const char *csvPath, const char *tableName)
    {
        // call to static function (if i comment this out it works ok)
        ParseCSV(csvPath, lines);

            //.....
    }

    // CREATE TABLE
    __declspec(dllexport) bool SQLite_CreateTable(const char *dbPath, const char *tableName, const char *fieldList)
    {
        // some code here...
    }
}

此处为 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace CPP_Library_To_NET
{
    class Program
    {
        [DllImport("Testlib.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool SQLite_CreateTable([MarshalAs(UnmanagedType.LPStr)]string dbPath, 
                                                     [MarshalAs(UnmanagedType.LPStr)]string tableName,
                                                     [MarshalAs(UnmanagedType.LPStr)]string fieldList);

        public static extern bool SQLite_ImportCSV([MarshalAs(UnmanagedType.LPStr)]string dbPath,
                                                   [MarshalAs(UnmanagedType.LPStr)]string csvPath,
                                                   [MarshalAs(UnmanagedType.LPStr)]string tableName);

        static void Main(string[] args)
        {
            // some variable initialisations...

            // Create table
            ret = SQLite_CreateTable(path, tableName, fieldList);

            if (!ret) { Console.WriteLine("Failed to create table " + tableName); }
            else { Console.WriteLine("Successfully created table " + tableName); }

            // Importing CSV data...
            ret = SQLite_ImportCSV(path, csvPath, "TestTab");

            if (!ret) { Console.WriteLine("Failed to import csv " + csvPath); }
            else { Console.WriteLine("Successfully imported csv " + csvPath); }

            Console.ReadLine();
        }
    }
}

关于如何解决这个问题有什么想法吗?

最佳答案

尝试像下面一样再次添加 DLLImport,看看它是否有效

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;

    namespace CPP_Library_To_NET
    {
        class Program
        {
            [DllImport("Testlib.dll", CallingConvention = CallingConvention.Cdecl)]
            public static extern bool SQLite_CreateTable([MarshalAs(UnmanagedType.LPStr)]string dbPath, 
                                                         [MarshalAs(UnmanagedType.LPStr)]string tableName,
                                                         [MarshalAs(UnmanagedType.LPStr)]string fieldList);

            //Try to add DLLImport again like below, see if it works
            [DllImport("Testlib.dll", CallingConvention = CallingConvention.Cdecl)]
            public static extern bool SQLite_ImportCSV([MarshalAs(UnmanagedType.LPStr)]string dbPath,
                                                       [MarshalAs(UnmanagedType.LPStr)]string csvPath,
                                                       [MarshalAs(UnmanagedType.LPStr)]string tableName);

            static void Main(string[] args)
            {
                // some variable initialisations...

                // Create table
                ret = SQLite_CreateTable(path, tableName, fieldList);

                if (!ret) { Console.WriteLine("Failed to create table " + tableName); }
                else { Console.WriteLine("Successfully created table " + tableName); }

                // Importing CSV data...
                ret = SQLite_ImportCSV(path, csvPath, "TestTab");

                if (!ret) { Console.WriteLine("Failed to import csv " + csvPath); }
                else { Console.WriteLine("Successfully imported csv " + csvPath); }

                Console.ReadLine();
            }
        }
    }

关于c# - 从外部函数 C++ DLL 调用静态函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19492514/

相关文章:

c++ - 打印十六进制?

c++ - 将 xml 字符串嵌套到 xml 元素中的语法

c# - 非托管 C++ 代码上的 NullReferenceException

c# - 如何在 Visual C++ 控制台应用程序中添加 C# dll?

c# - 如何以像素增量滚动 DataGridView

c# - 压缩/解压缩音频数据

c++ - 自定义容器适用于 g++(Rstudio),不适用于 MSVS 2012

C# DLLImport 'Complex' 数组返回和参数

c# - 从自定义用户控件基类派生用户控件

c# - WPF:轮询键盘