感谢所有回答的人,每个回答都有一点答案。因此,为了那些可能会遇到相同问题的人,我将发布我编写的代码以尝试我所需的链接。
//以下是C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("dodll.dll",
CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPStr)]
private static extern string dodll(int a, int b,
[MarshalAs(UnmanagedType.LPArray)] float[] nums,
[MarshalAs(UnmanagedType.LPStr)] string strA,
[MarshalAs(UnmanagedType.LPStr)] string strB);
static void Main(string[] args)
{
int x = 2; int y = 3;
float[] numbers = new float[3]{12.3F, 45.6F, 78.9F};
float[] simargs = new float[20];
string strvarA = "Test string A";
string strvarB = "Test another string";
string answer = dodll(x, y, numbers, strvarA, strvarB);
Console.WriteLine("hello world " + answer);
Console.WriteLine("another hello world" + x);
Console.WriteLine("End of sim");
}
//这是C代码(DLL):
#include <stdio.h>
char astring[50];
extern "C"
{ __declspec(dllexport)
char* dodll( long a, long b, float* aptr, char* sa, char* sb)
{
float nums[3];
nums[0] = *aptr;
nums[1] = *(aptr+1);
nums[2] = *(aptr+2);
sprintf(astring, "Building string: %s %s %ld, nrs are %2.1f, %2.1f, %2.1f.\n",
sa, sb, (a+b), nums[0], nums[1], nums[2]);
printf("Inside DLL: %s\n", astring);
return astring;
}
}
DllImport
属性。这里有一个带有 C#/C 示例的链接(向下滚动):http://www.csharphelp.com/2006/12/c-tutorial-for-beginners/ - user568493