3

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!

Community
  • 1
  • 1
Paul Kinzelman
  • 547
  • 1
  • 7
  • 22
  • By "your project", he is talking about the C/C++ DLL project; and "consumer projects" are those that will use the DLL. His example assumes the consumer uses C++ to import the DLL, but your consumer is C#, which has a similar (but different) import mechanism: http://msdn.microsoft.com/en-us/library/aa984739%28VS.71%29.aspx –  Jan 18 '11 at 07:47
  • Also, you need a DLL export of your C code in order for C# to use it. `returntype` is not a reserved word; for export, `MYPROJECTAPI(int) foo();` expands to this text: `extern "C" int __stdcall __declspec(dllexport) foo();`. That `returntype` param is silly because the return type doesn't have to precede `__stdcall __declspec`. You can actually remove the param from the macro altogether and use it like this: `MYPROJECTAPI int foo();` –  Jan 18 '11 at 07:54
  • If I'm reading it right, that msdn link is for C++->C link, is there anything similar illustrating the magical incantation detail for C#->C? – Paul Kinzelman Jan 19 '11 at 00:50
  • 1
    Oh, you're right. In any case, C# also uses the `DllImport` attribute. Here is a link with a C#/C example (scroll to bottom): http://www.csharphelp.com/2006/12/c-tutorial-for-beginners/ –  Jan 19 '11 at 05:55
  • Yes, that's more like it! I'll have to digest this when I get to needing it. I am surprised that they'd include a C#/C link example in a beginner tutorial, sounds like it's a pretty common thing to do then. – Paul Kinzelman Jan 19 '11 at 19:42
  • I would look up 'marshalling', too. The thing is: variables in the managed code (C# has a runtime that manages your code as it executes) can be moved around by the runtime at will, and clean them up at will, too (the C# 'equivalent' of C++'s 'free()' is 'garbage collection' - a whole 'special thread' for your process thing) ... the C# runtime for marshalling is *very* powerful but strange things happen if/when you start passing pointers around. – Aidanapword Mar 10 '11 at 16:06
  • I hope that you aren't trying this for Windows Phone 7 as your C dll won't run there. – Julien Roncaglia Mar 10 '11 at 16:15

3 Answers3

3

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;
}
}
Javed Akram
  • 15,024
  • 26
  • 81
  • 118
Paul Kinzelman
  • 547
  • 1
  • 7
  • 22
0

I would start here: MSDN on Interop

... be sure to choose the requisite VS edition (2005/2008/2010) ... :)

Aidanapword
  • 288
  • 1
  • 13
  • Everything I heard elsewhere said that Windows Phone does not do C#-C linkage, so if you have information to the contrary, please let me know. I didn't find anything about that in the link you gave. Thanks! – Paul Kinzelman Sep 06 '11 at 04:33
0

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.

Paul Kinzelman
  • 547
  • 1
  • 7
  • 22