Calling a static function from an extern function C++ DLL
I have written a C++ DLL with 2 extern "C" functions included within it as well as a static function which is called from within one of the extern functions. When I try to call the extern functions from C#, the one which calls the static function (also included in the DLL) I get a build warning ("Method, operator or accessort '...' is marked external and has no attributes in it. Consider adding a DllImport attribute to specify the external implementation.). When I comment out the static function call, everything compiles smoothly. Any ideas on how to fix this? Do I have to use extern on the static function as well?
My code is as below:
#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# Code here:
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();
}
}
}
Any ideas on how to fix this?
尝试像下面一样再次添加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/44464.html