从外部函数C ++ DLL调用一个静态函数
我写了一个C ++ DLL,其中包含2个extern“C”函数,以及一个从其中一个extern函数中调用的静态函数。 当我尝试从C#中调用外部函数时,调用静态函数(也包含在DLL中)的函数会得到一个构建警告(“Method,operator或accessort'...''被标记为external,并且没有属性它考虑添加一个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();
}
}
}
链接地址: http://www.djcxy.com/p/44463.html
上一篇: Calling a static function from an extern function C++ DLL
下一篇: LINQPad: Attempting to use assembly that uses [DllImport] to access C++ dll