C # - C Compatibility

The Q / A link in is very close to what I'm looking for, but I'm just starting with C # and you need to fill out a little more and maybe some hints for a better way to continue.

I have an application that I wrote for PalmPre / webOS in Javascript, and part of it is written in C for portability, not for performance. It performs Lear Jet performance calculations.

In the webOS world, C code (plugin) goes in its own process and there is a way for JS to call and call C code (using "main") to start the process, and C can register entry points. Then JS can call the entry point with some arguments, the code C performs the calculation, and then C returns a pointer to a string of digits in JS for display. C code does not have graphics, does not allocate dynamic memory, etc. I would like to essentially convert the JS GUI code to C # and use the C code with a few changes (# if's) for C # to do the same thing that JS on Pre now does.

Answer 1 / option2 I think this is the best, but I did not understand what he meant by “your project versus consumer project” and how / why it means that one of them is dllimport and one is dllexport, and I do not have a DLL, I only have C-code routines. It seems like all I have to do is replace it with “PublicFunc” with my C routine, right? And could I have a few arguments saying "params"? However, the type of the return type is not specified, how would I return a response to C #? Is 'returntype' a reserved word? Or an example of a place owner? Or am I off track because I don't have a DLL? BTW, C code has a compilation mode to run autonomously as a DOS program for testing.

- , , ? MS VS 2010 Express , . , - ?

!

+3
3

, , . , , , , , .

// #:

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):

#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;
}
}
+3

: MSDN Interop

... VS (2005/2008/2010)...:)

0

... , Windows Phone 7 #/XAML C/++, , . , #, #, Android.

0

Source: https://habr.com/ru/post/1786034/


All Articles