我编写了一个包含2个extern“C”函数的C ++ DLL,以及一个从extern函数中调用的静态函数。当我尝试从C#中调用extern函数时,调用静态函数的函数(也包含在DLL中)我得到一个构建警告(“Method,operator或accessort'......'标记为external,并且没有属性考虑添加一个DllImport属性来指定外部实现。)。当我注释掉静态函数调用时,一切都顺利编译。有关如何解决这个问题的想法吗?我还必须在静态函数上使用extern吗? / p>
我的代码如下:
#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();
}
}
}
有关如何解决此问题的任何想法?
答案 0 :(得分: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();
}
}
}