The usage of C++ Intermodulation
This article mainly introduces "the use of C++ Intermodulation". In daily operation, I believe many people have doubts about the use of C++ Intermodulation. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about the use of C++ Intermodulation! Next, please follow the editor to study!
# if! defined (_ _ CCALLDLL2__) # define _ _ CCALLDLL2__#ifdef C_API_EXPORTS#define C_API_TEST extern _ _ declspec (dllexport) # else#define C_API_TEST extern _ declspec (dllimport) # endif / / C_API_EXPORTS#ifdef _ cplusplus extern "C" {# endif / / # define extern "C" _ declspec (dllimport) struct user {int id; int age }; struct structpointer {int id; int* ptrid;}; typedef void (* cbfun1) (int x, int y); typedef int (* cbfun2) (int x, int y); struct alignteststruct {int v1; char v2; char v3 Int v4;}; struct alignteststruct2 {int v1; char v2; char* v3; int v4;}; struct alignteststruct3 {int v1; char v2; char* v3 [3] Int v4;}; C_API_TEST int say (int u); C_API_TEST int saystruct (user u); C_API_TEST int retstruct (user* u, int x, int y); C_API_TEST user* retstructptr (user* u, int x, int y); C_API_TEST void setstructs (user u [], int len, int x, int y) C_API_TEST void processstructpointer (structpointer * sp); C_API_TEST void processstructpointer2 (structpointer * sp, int len); C_API_TEST void cbfundemo (user* u, int x, int y, cbfun1 cb); C_API_TEST int cbfundemo2 (user* u, int x, int y, cbfun2 cb); C_API_TEST void dispalign (alignteststruct ats); C_API_TEST void dispalign2 (alignteststruct2 ats) C_API_TEST void dispalign3 (alignteststruct3 ats); C_API_TEST void dispalign4 (alignteststruct3 ats, int v3len); C_API_TEST void xxxByReference (char* C1, short* S1, int* i1, long* L1, float* F1, double* D1); C_API_TEST char* strtocharptr (char* arg); C_API_TEST void deletepr (); C_API_TEST int globalvar / / public Pointer getGlobalVariableAddress (java.lang.String symbolName) C_API_TEST char * test1 (); C_API_TEST char * test2 (); # ifdef _ _ cplusplus} # endif#endif// ccalldll2.cpp: define the export function of the DLL application. / / # include "stdafx.h" # include / / # using "cSharpDLLforJNA.dll" using namespace System;using namespace cSharpDLLforJNA;using namespace std;//#pragma data_seg ("MyData") / / No effect int globalvar=58; / / public Pointer getGlobalVariableAddress (java.lang.String symbolName) / / # pragma data_seg () / / https://www.cnblogs.com/luzhiyuan/p/3947576.html mentions extern int gcorrecnTest; and extern int _ declspec (dllimport) g_nTest / / if you don't use _ declspec (dllimport), it represents the address, and if it is used, it represents the real variable. If so, can it be used for hackerint say (int u) {/ * gcnew User (); * / Main ^ dllmain = gcnew Main (); user user; user.age = 20; user.id = 1; int r = dllmain- > say (u) Return r;} int saystruct (user u) {int r = u.id + u.age; return r;} int retstruct (user* uther int x int y) {/ / directly process on u and return this u, then the original parameter user in java should be the same as the returned user. Do experiments to see u-> id + = 10 + x; u-> age + = 10 + y. Return 0;} user* retstructptr (user* u, int x, int y) {/ / directly processes on u and returns this u, then the original parameter user in java should be the same as the returned user. Do experiments to see u-> id + = 20 + x; u-> age + = 20 + y; cout age) } int cbfundemo2 (user* u, int x, int y, cbfun2 cb) {u-> id + = x; u-> age + = y; return cb (u-> id, u-> age);} void dispalign (alignteststruct ats) {cout