上位机开发--C#调用C做的库dll

void Swap(int* a, int* b)
{
    int c = *a;
    *a = *b;
    *b = c;
}

https://www.cnblogs.com/notnolyblogs/p/6015933.html
错误1 Swap首字母大写!
错误2 原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。
参考:https://blog.csdn.net/jacke121/article/details/78617972
  [DllImport("test.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
  可以了!!
  
        private void button1_Click(object sender, EventArgs e)
        {
            int a = 1;
            int b = 2;

            unsafe
            {
                Swap(&a, &b);
        }}
        debug看到执行了 !!!!
        
进阶:
危胜中的des解压以后 注销table.h里面//extern static BoolType SubKey[16][48];     

+++++++++++++++++++

cmd

cd C:\MinGW\bin

gcc -shared -o DES.dll DESalgorithm.c

问题 可以KEIL加载吗?

 

++++++++++++++++++   
然后gcc -shared -o DES.dll DESalgorithm.c(在目录下启动CMD 在目录下放置3个文件)
出来了dll
第一步加入:
  public partial class Form1 : Form
    {

        [DllImport("DES.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public unsafe static extern void PlayDes(byte[] MesOut, byte[] MesIn);

        [DllImport("DES.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public unsafe static extern void SetKey(byte[] KeyIn);
第二部:调用1
  public Form1()
        {
            InitializeComponent();
            byte[] pwd = new byte[8];
            pwd[0] = pwd[1] = pwd[2] = pwd[3] = pwd[4] = pwd[5] = pwd[6] = pwd[7] = 0xFF;
            SetKey(pwd);
        }
        第二部:调用2
        private void button1_Click(object sender, EventArgs e)
        {
            byte[] IN = new byte[8];
            byte[] OUT =new byte[8];
            IN[0] = IN[1] = IN[2] = IN[3] = IN[4] = IN[5] = IN[6] = IN[7] =0X11;
            unsafe
            {
                PlayDes(OUT,IN);
            }

        }
        看到结果正确!!!!!!

你可能感兴趣的:(上位机开发--C#调用C做的库dll)