C# - Ç互操作性 [英] C# - C interoperability

查看:164
本文介绍了C# - Ç互操作性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接文字中的Q / A非常接近我正在寻找的,但我只是从C#开始,需要一些更多的填充,并可能一些提示关于最好的方式继续。

The Q/A at link text gets very close to what I'm looking for, but I'm just starting on C# and need a bit more filled in and possibly some hints about the best way to proceed.

我有一个应用程序我写的PalmPre / webOS在Javascript中,它的一部分写在C的可移植性,而不是性能。它执行Lear Jet的性能计算。

I've got an app I've written for the PalmPre/webOS in Javascript and a piece of it is written in C for portability, not for performance. It does Lear Jet performance calculations.

在webOS世界中,C代码(插件)在自己的进程中,有一种方法让JS调用和调用C代码(使用'main')启动进程,C可以注册入口点。然后JS可以调用带有一些参数的entrypoint,C代码执行计算,然后C返回一个指向JS的数字字符串的指针以显示。 C代码没有图形,没有动态内存分配等。我想从本质上将JS GUI代码转换为C#,并使用C代码与微小的调整(#if's)为C#做同样的事情,JS

In the webOS world, the C code (plugin) goes in its own process and there's a way for JS to invoke and invoke the C code (using 'main') to start the process and C can register entry points. Then JS can call an entrypoint with some arguments, the C code does a calculation, and then C returns a pointer to a string of digits to JS for display. The C code has no graphics, no dynamic memory allocation, etc. I'd like to essentially convert the JS GUI code into C#, and use the C code with minor tweaks (#if's) for C# to do the same thing that JS on the Pre does now.

答案1 / option2我认为最好,但我不明白他的意思是什么你的项目vs消费者项目,以及如何/为什么这意味着一个是dllimport,一个是dllexport,我没有DLL,我只有C代码例程。看起来我只需要替换他的'publicFunc'我的C程序,对吧?我可以有一些参数,它说params?但是没有指定返回类型,我如何返回一个答案C#? returntype是保留字吗?还是示例占位符?还是我离开轨道,因为我没有DLL? BTW,C代码确实有一个编译模式,作为一个DOS程序单独运行测试。

Answer 1/option2 I think is best, but I didn't understand what he meant by "your project vs consumer project" and how/why that means one is a dllimport and one is a dllexport, and I don't have a DLL, I have just C code routines. It looks like all I'd have to do is replace his 'PublicFunc' with my C routine, right? And I could have a number of args where it says 'params'? However there's no return type specified, how would I return an answer to C#? Is 'returntype' a reserved word? Or an example place-holder? Or am I off the track because I don't have a DLL? BTW, the C code does have a mode to compile to run stand-alone as a DOS program for testing.

有一个简单的示例代码,说明如何做这个?我现在正在下载的MS VS 2010 Express,还没有安装它。也许有什么东西吗?

Is there any simple example code someplace which illustrates how to do this? I'm downloading the MS VS 2010 Express now, haven't installed it yet. Perhaps there's something there?

TIA!

推荐答案

谁回答,一切都有一小块的答案。所以对于那些可能跟着我的同一个问题,我会发布我写的代码实验链接为我所需要的。

Thanks to everybody who answered, everything had little pieces of the answer. So for those who might come after me with the same question, I'll post the code I wrote to experiment with the linkage for what I needed.

//这里是C#代码:

//Here's the C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;     // DLL support
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):

//Here's the C code (the DLL):

#include <stdio.h>
char astring[50];
//Passing structure pointer info: http://www.adp-gmbh.ch/win/misc/mingw/dll.html
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;
}
}

这篇关于C# - Ç互操作性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆