0

我编写了一个 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();
        }
    }
}

有想法该怎么解决这个吗?

4

1 回答 1

1

尝试像下面一样再次添加 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();
            }
        }
    }
于 2013-10-21T10:57:40.877 回答