简体   繁体   中英

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.

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.

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.

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.

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.

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

} }


//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;
}
}

I would start here: MSDN on Interop

... be sure to choose the requisite VS edition (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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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