Answer the question
In order to leave comments, you need to log in
How to code a C-like DLL in C# (which can be dynamically loaded)?
For example, in C, the export of functions looks like this:
extern "C" __declspec(dllexport) int Run() {
return 488;
}
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace wfa
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyClass.LoadDll(@"D:\dev\csharp\test\Debug\cpx.dll");
var result = MyClass.Run().ToString();
MessageBox.Show(result, result);
}
}
public class FunctionLoader
{
[DllImport("Kernel32.dll")]
private static extern IntPtr LoadLibrary(string path);
[DllImport("Kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
public static Delegate LoadFunction<T>(string dllPath, string functionName)
{
var hModule = LoadLibrary(dllPath);
var functionAddress = GetProcAddress(hModule, functionName);
return Marshal.GetDelegateForFunctionPointer(functionAddress, typeof(T));
}
}
public class MyClass
{
public delegate int FnDelegate();
public static FnDelegate Run;
public static void LoadDll(string path)
{
Run = (FnDelegate)FunctionLoader.LoadFunction<FnDelegate>(path, "Run");
}
}
}
Answer the question
In order to leave comments, you need to log in
I don't know what form the response comes in, but anyway - you need to parse the data from the string into the object, and then access its field, below is an example
function getCityName(json){
var obj = JSON.parse(json)
var cityName = obj['city_name'] // предположим, что в json нужное нам поле называется "city_name"
return cityName;
}
This is a development of what you have tried.
There is a Nuget package.
C# is not designed for such tasks
If you need to make calls from native code to NET assemblies, use COM or C++ CLI wrappers
Robert Giesecke's Unmanaged Exports
solution doesn't build, compile-time errors occur
class Test
{
[DllExport("add", CallingConvention = CallingConvention.Cdecl)]
public static int Run()
{
return 1082;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question