How to make a callback to C# from C/C++ code
来源:互联网 发布:sql server 2012版本 编辑:程序博客网 时间:2024/06/11 20:35
How to make a callback to C# from C/C++ code
26 Jun 2013 CPOL 60.1K 66
, 33 votes) (
This post shows how to make a callback to C# from C/C++
Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email sent so we can confirm your email address and start sending you newsletters again. Alternatively, you can update your subscriptions.
Almost everyone knows how to make a call to a function in an unmanaged DLL. However, sometimes we wish that we could call C# code from C/C++ code.
Imagine a scenario wherein we have a C# application which has a native C DLL called Engine.dll. There is a function entry named “
This will work just fine. However, let’s assume
Engine.dll/Main.h
Engine.dll/Main.c
TestApp.exe/Program.cs
Enjoy it
Imagine a scenario wherein we have a C# application which has a native C DLL called Engine.dll. There is a function entry named “
DoWork
” in this DLL that we need to call. Calling DoWork
in the engine is as easy as making the following declaration in the C# code:Hide Copy Code
[DllImport("Engine.dll")]public static extern void DoWork();
…and then using it like any other static
C# method in our C# application.This will work just fine. However, let’s assume
DoWork
is a long-running task and we want to show a progress or so in the C# app in order to keep our user(s) updated. To make this happen, we need to…- Define an unmanaged delegate in the C# code like –Hide Copy Code
[UnmanagedFunctionPointer(CallingConvention.StdCall)]delegate void ProgressCallback(int value);
- Define callback signature in the C code –Hide Copy Code
typedef void (__stdcall * ProgressCallback)(int);
- Change
DoWork
signature in C code to acceptProgressCallback
address:Hide Copy CodeDLL void DoWork(ProgressCallback progressCallback)
Note: DLL is…Hide Copy Code#define DLL __declspec(dllexport)
- Inside the C# code, we need to create a delegate of type of the unmanaged delegate –Hide Copy Code
ProgressCallback callback = (value) => { Console.WriteLine("Progress = {0}", value); };
- Then for calling
DoWork
, we need to do it like this –Hide Copy CodeDoWork(callback);
ProcessFile
that needs to get back to the C# in order to obtain a file path for further processing - in this case, printing its contents to the console.Engine.dll/Main.h
Hide Copy Code
#include "Windows.h"#ifdef __cplusplusextern "C"{#endif #define DLL __declspec(dllexport) typedef void (__stdcall * ProgressCallback)(int); typedef char* (__stdcall * GetFilePathCallback)(char* filter); DLL void DoWork(ProgressCallback progressCallback); DLL void ProcessFile(GetFilePathCallback getPath); #ifdef __cplusplus}#endif
Engine.dll/Main.c
Hide Shrink Copy Code
#include "Main.h"#include <stdio.h>DLL void DoWork(ProgressCallback progressCallback){ int counter = 0; for(; counter<=100; counter++) { // do the work... if (progressCallback) { // send progress update progressCallback(counter); } }} DLL void ProcessFile(GetFilePathCallback getPath){ if (getPath) { // get file path... char* path = getPath("Text Files|*.txt"); // open the file for reading FILE *file = fopen(path, "r"); // read buffer char line[1024]; // print file info to the screen printf("File path: %s\n", path ? path : "N/A"); printf("File content:\n"); while(fgets(line, 1024, file) != NULL) { printf("%s", line); } // close the file fclose(file); }}
TestApp.exe/Program.cs
Hide Shrink Copy Code
using System;using System.Runtime.InteropServices;using System.Windows.Forms; class Program{ [UnmanagedFunctionPointer(CallingConvention.StdCall)] delegate void ProgressCallback(int value); [UnmanagedFunctionPointer(CallingConvention.StdCall)] delegate string GetFilePathCallback(string filter); [DllImport("Engine.dll")] public static extern void DoWork([MarshalAs(UnmanagedType.FunctionPtr)] ProgressCallback callbackPointer); [DllImport("Engine.dll")] public static extern void ProcessFile([MarshalAs(UnmanagedType.FunctionPtr)] GetFilePathCallback callbackPointer); [STAThread] static void Main(string[] args) { // define a progress callback delegate ProgressCallback callback = (value) => { Console.WriteLine("Progress = {0}", value); }; Console.WriteLine("Press any key to run DoWork...."); Console.ReadKey(true); // call DoWork in C code DoWork(callback); Console.WriteLine(); Console.WriteLine("Press any key to run ProcessFile...."); Console.ReadKey(true); // define a get file path callback delegate GetFilePathCallback getPath = (filter) => { string path = default(string); OpenFileDialog ofd = new OpenFileDialog() { Filter = filter }; if (ofd.ShowDialog() == DialogResult.OK) { path = ofd.FileName; } return path; }; // call ProcessFile in C code ProcessFile(getPath); }}
Enjoy it
0 0
- How to make a callback to C# from C/C++ code
- How to make a callback to C# from C/C++ code
- How to pass macro definition from “makefile” command line arguments to C source code?
- How to make a random selection from an SQL table
- how to make a good resume from Google Engineer
- 360iDev: How to make money from a free app
- android - How to make a phone call from webview
- How to make a sprite from URL in cocos2d-x
- How to study C && ASM Code(1)
- How to study C && ASM Code(2)
- How to study C && ASM Code(3)
- How to study C && ASM Code(6)
- How to study C && ASM Code(4)
- How To Make A Keygen
- How to make a patch
- how-to-make-a-rpg
- How to make a ipcamera
- How to call an Objective-C Method from a C Method?
- osgi服务报错以及相应的解决方法
- 国产手机高价就提升档次了?超越苹果是痴人说梦
- 我绝对不会破坏现在的生活了
- js判断一个数组中是否有重复的值
- 【OpenCV】SIFT原理与源码分析
- How to make a callback to C# from C/C++ code
- 用可变参数列表求平均值
- javascript中return的作用
- Intellij Ideay一、开启自动编译
- base64_to_jpeg
- 【OpenCV】SIFT原理与源码分析:1.DoG尺度空间构造
- 九章算法面试题6 负载均衡
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- 10个有关String的面试问题