VC的DLL配合C#達到動態配置記憶體 & DLL使用RUNDLL32標準測試語法
VC的DLL配合C#達到動態配置記憶體 & DLL使用RUNDLL32標準測試語法
GITHUB: https://github.com/jash-git/jashliao_VC/tree/master/002_DLL%E5%AE%8C%E6%95%B4%E8%A8%AD%E8%A8%88%E7%AF%84%E4%BE%8B/15_VC%E7%9A%84DLL_C%23%E5%8B%95%E6%85%8B%E9%85%8D%E7%BD%AE%E8%A8%98%E6%86%B6%E9%AB%94%E5%92%8CDLL%E4%BD%BF%E7%94%A8RUNDLL32%E6%A8%99%E6%BA%96%E6%B8%AC%E8%A9%A6%E8%AA%9E%E6%B3%95
[jashliao_VC/002_DLL完整設計範例/15_VC的DLL_C#動態配置記憶體和DLL使用RUNDLL32標準測試語法/]
VC的DLL
H檔案 |
#ifndef c_MathFuncsDll_H
#define c_MathFuncsDll_H
#include <stdio.h>
typedef struct _Data
{
bool bData;
int nData;
char strData[256];
} Data;
struct Bar
{
public :
int id;
char*
};
extern “C”{
__declspec(dllexport) int Test_AllocData(int count, Data*& res);
__declspec(dllexport) int Test_FreeData(Data* d);
__declspec(dllexport) int GetBar(Bar& bar);
__declspec(dllexport) int SaveByteArray(BYTE * pArray, int nSize);
// Returns a +
__declspec(dllexport) double Add(double a, double b);
// Returns a – b
__declspec(dllexport) double Subtract(double a, double b);
// Returns a * b
__declspec(dllexport) double Multiply(double a, double b);
// Returns a / b
__declspec(dllexport) double Divide(double a, double b);
__declspec(dllexport) void EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);
}
#endif
|
CPP檔 |
// c_MathFuncsDll.cpp : 定w義Mq DLL 應3用DI程g式!的o匯¡Ñ出DX函Lc式!。C
//
#include “stdafx.h”
#include <stdio.h>
#include “c_MathFuncsDll.h”
int Test_AllocData(int count, Data*& res)
{
res = new Data[count];
for(int i=0 ; i<count ; i++)
{
/*
http://falldog7.blogspot.tw/2015/01/c-sharp-allocate-structure-buffer-array-dll.html
*/
res[i].bData = true;
res[i].nData = 500 + i;
sprintf_s(res[i].strData, “Test_AllocData(%d)”, i);
}
return 0;
}
int Test_FreeData(Data* d)
{
/*
http://falldog7.blogspot.tw/2015/01/c-sharp-allocate-structure-buffer-array-dll.html
*/
if(d)
{
delete []
}
return 0;
}
int GetBar(Bar& bar)
{
/*
http://www.cnblogs.com/yukaizhao/archive/2011/04/27/csharp_call_cpp_class_struct.html
*/
FILE *pf;
pf=fopen(“data.txt”,“a”);
fprintf(pf,“bar.id=%d\tbar.name=%s\n”,bar.id,bar.name);
fprintf(pf,“\n”);
fclose(pf);
bar.id = 10;
bar.name = “hi
return 0;
}
int SaveByteArray(BYTE * pArray, int nSize)
{
/*
https://social.msdn.microsoft.com/Forums/en-US/32f1a857-91d6-472d-b333-469ed7f6f6dd/passing-a-byte-array-to-a-c-dll-from-c?forum=csharplanguage
*/
FILE *pf;
pf=fopen(“data.txt”,“a”);
for (int i=0; i<nSize; i++)
{
fprintf(pf,“%d,”,
}
fprintf(pf,“\n”);
fclose(pf);
return 0;
}
double Add(double a, double b)
{
return a
}
double Subtract(double a, double b)
{
return a
}
double Multiply(double a, double b)
{
return a
}
double Divide(double a, double b)
{
if (b ==
{
return -1;
}
return a
}
void EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR
{
//https://support.microsoft.com/zh-tw/kb/164787
/*
原i文a說!明u
RUNDLL.EXE
hwnd = (parent window handle)
hinst = HINSTANCE of SETUPX.DLL
lpszCmdLine = “132
nCmdShow = (whatever the nCmdShow
*/
//測¦u試MO:RUNDLL32.EXE
FILE *pf;
pf=fopen(“123.txt”,“w”);
fprintf(pf,“%s”,lpszCmdLine);
fclose(pf);
}
_ |
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;//USE DLL
namespace ConsoleApplication1
{
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct Bar
{
/// int
public int id;
/// char*
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public string name;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Data
{
public bool bData;
public int nData;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string strData;
}
class Program
{
//*
[DllImport(“./c_MathFuncsDll.dll”,
public static extern void Test_AllocData(int size, ref IntPtr ptr);
[DllImport(“./c_MathFuncsDll.dll”,
public static extern void Test_FreeData(IntPtr ptr);
[DllImport(“./c_MathFuncsDll.dll”,
extern static void GetBar(ref Bar bar);
[DllImport(“./c_MathFuncsDll.dll”,
static extern Int32 SaveByteArray(IntPtr pArray, int nSize);
[DllImport(“./c_MathFuncsDll.dll”,
static extern double Add(double a, double b);
[DllImport(“./c_MathFuncsDll.dll”,
static extern double Subtract(double a, double b);
[DllImport(“./c_MathFuncsDll.dll”,
static extern double Multiply(double a, double b);
[DllImport(“./c_MathFuncsDll.dll”,
static extern double Divide(double a, double b);
//*/
/*
public static extern int
public static extern int
*/
static void Pause()
{
Console.Write(“Press any key to continue . . . “);
Console.ReadKey(true);
}
static void Main(string[]
{
///*
double d1 = 5.0d;
double d2 = 20.0d;
double d3=0.0;
d3 = Add(d1, d2);
Console.WriteLine(“HELLO WORD”);
Console.WriteLine(“d1=” + d1);
Console.WriteLine(“d2=” + d2);
Console.WriteLine(“d3=” + d3);
//============================
byte[]
for (int i = 0; i < 16; i++)
{
array[i] = (byte)(i + 97);
}
int size = Marshal.SizeOf(array[0]) *
IntPtr pnt = Marshal.AllocHGlobal(size);
try
{
//
Marshal.Copy(array,
}
finally
{
//
//
Console.WriteLine(“C# data 2 C++ error”);
}
SaveByteArray(pnt, array.Length);
Marshal.FreeHGlobal(pnt);
//============================
Bar b = new Bar();
b.id = 100;
b.name = “hi
GetBar(ref b);
Console.WriteLine(“b’s id is “ + b.id);
Console.WriteLine(“b’s name is “ + b.name);
//
IntPtr binary = new IntPtr();
{
Data[]
Test_AllocData(3, ref binary);
ad[0] = (Data)Marshal.PtrToStructure(binary
ad[1] = (Data)Marshal.PtrToStructure(binary
ad[2] = (Data)Marshal.PtrToStructure(binary
Test_FreeData(binary);
for (int i = 0; i < 3; i++)
{
Console.WriteLine(string.Format(“@AllocData
}
}
Console.WriteLine(“Call C++ DLL finish”);
//*/
/*
_flushall();
*/
Pause();
}
}
}
|
RUNDLL32標準測試
RUNDLL32.EXE |
One thought on “VC的DLL配合C#達到動態配置記憶體 & DLL使用RUNDLL32標準測試語法”
C# 呼叫 C/C++ DLL
指標 動態配置記憶體
RUNDLL32標準測試語法