简体   繁体   English

C# - C互操作性

[英]C# - C interoperability

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. 链接文本的Q / A非常接近我正在寻找的内容,但我只是开始使用C#并且需要更多填写,并且可能有一些关于最佳方法的提示。

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. 我有一个我用Javascript写的PalmPre / webOS的应用程序,其中一部分用C语言编写,以实现可移植性,而不是性能。 It does Lear Jet performance calculations. 它做Lear Jet性能计算。

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. 在webOS世界中,C代码(插件)进入自己的进程,JS有一种方法可以调用和调用C代码(使用'main')来启动进程,C可以注册入口点。 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. 然后JS可以使用一些参数调用入口点,C代码进行计算,然后C返回指向JS的数字字符串的指针以供显示。 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. C代码没有图形,没有动态内存分配等。我想基本上将JS GUI代码转换为C#,并使用C代码对C#进行微调(#if's)来做与JS相同的事情。 Pre现在做了。

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. 答案1 / option2我认为最好,但我不明白他的意思是“你的项目与消费者项目”以及如何/为什么这意味着一个是dllimport,一个是dllexport,我没有DLL ,我只有C代码例程。 It looks like all I'd have to do is replace his 'PublicFunc' with my C routine, right? 看起来我要做的就是用我的C例程替换他的'PublicFunc',对吧? And I could have a number of args where it says 'params'? 我可以有一些args,它说'params'? However there's no return type specified, how would I return an answer to C#? 但是没有指定返回类型,我将如何返回C#的答案? Is 'returntype' a reserved word? “返回型”是一个保留字吗? Or an example place-holder? 或者一个例子占位符? Or am I off the track because I don't have a DLL? 或者我离开赛道因为我没有DLL? BTW, the C code does have a mode to compile to run stand-alone as a DOS program for testing. 顺便说一下,C代码确实有一种编译模式可以独立运行作为DOS程序进行测试。

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. 我现在正在下载MS VS 2010 Express,还没有安装它。 Perhaps there's something there? 也许那里有什么东西?

TIA! 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. 所以对于那些可能会跟我问相同问题的人,我会发布我写的代码来试验我需要的链接。

//Here's the C# code: //这是C#代码:

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");
}

} } }}


//Here's the C code (the DLL): //这是C代码(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;
}
}

I would start here: MSDN on Interop 我将从这里开始: 在Interop上的MSDN

... be sure to choose the requisite VS edition (2005/2008/2010) ... :) ...一定要选择必备的VS版(2005/2008/2010)...... :)

Addendum... I recently found that Windows Phone 7 C#/XAML does NOT support linking with C/C++ so that blows what I wanted to do out of the water. 附录......我最近发现Windows Phone 7 C#/ XAML不支持使用C / C ++进行链接,因此无法实现我想要做的事情。 Apparently everything has to be in C# and I'm not going to rewrite all my graphical analysis code and database in C# so I'll move on to Android. 显然一切都必须在C#中,我不打算用C#重写我所有的图形分析代码和数据库,所以我将继续使用Android。

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

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