Tuesday, November 25, 2025

c++ - How to make .dll and call it from c#

c++ DLL cpp:
extern "C" __declspec( dllexport )
int math_add( int const d1, int const d2 ) {
    return d1 + d2;
}
Test c++ console app:
#include 

extern "C" __declspec( dllimport ) int math_add( int, int );

int main( int argc, char* argv[] )
{
    int i = math_add( 4, 5 );

    std::cout << '\n' << i << '\n';
    
    std::cout << "Press any key..";

    std::cin.get();

    return 0;
}
C# class:
using System.Runtime.InteropServices;

namespace test {
    public class PILib {

        [DllImport( "piMath.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int math_add( int _v1, int _v2);

    }
}
Calling in c#:
private void button1_Click( object sender, EventArgs e ) {
    int i = PILib.math_add( 3, 5 );

    MessageBox.Show( i.ToString() );
}
Output:

Tuesday, November 4, 2025

c# - How to call Min(), Max() on generic List

DateTime minStartTime = 
ListOfProcesesForKilling.Min( p => {        
  return p.StartTime;
} );
value += $"Min starttime: {minStartTime};";

c# - How to force JSON enum to save as TEXT instead of a NUMBER

This enum property:
public enum MinimizeActionEnum {
  NONE = 0,
  CLEAR = 1
}
    
public MinimizeActionEnum MinimizeAction { get; set; } = MinimizeActionEnum.NONE;
It is saved as INT by default:
"ReportParams": [
{
  "Name": "pProdId",
  "Description": "",
  "MinimizeAction": 0
},
With using that attribute:
[JsonConverter(typeof(JsonStringEnumConverter))]
public MinimizeActionEnum MinimizeAction { get; set; } = MinimizeActionEnum.NONE;
the enum value will be stored as TEXT:
"ReportParams": [
{
  "Name": "pProdId",
  "Description": "",
  "MinimizeAction": NONE
},