简体   繁体   English

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

[英]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. 我已经编写了一个C ++ DLL,其中包括2个extern“ C”函数以及从其中一个extern函数中调用的静态函数。 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? 当我尝试从C#调用extern函数时,调用静态函数(也包含在DLL中)的外部函数会收到生成警告(“ Method,operator或accessort'...'被标记为外部且在其中没有属性)考虑添加一个DllImport属性以指定外部实现。)当我注释掉静态函数调用时,所有内容都可以顺利编译。有关如何解决此问题的任何想法?我也必须在静态函数上使用extern吗?

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: 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();
        }
    }
}

Any ideas on how to fix this? 有想法该怎么解决这个吗?

Try to add DLLImport again like below, see if it works 尝试再次添加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();
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM