gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:20:47 25 4
gpt4 key购买 nike

我编写了一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com