Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use data transformations commonly used in CGO projects

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use the data conversion commonly used in CGO projects". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use data conversion commonly used in CGO projects".

Preface

Need to deploy the relevant environment and have basic knowledge points, this is not a popular science article, mainly for the actual project used in the type conversion and use, for the dynamic library function call parameter transfer and reception

1. GO environment, start to support CGO

2. Install glossy + beforehand

3. Understand the grammar of GO and C

4. You'd better know the basic makefile or shell syntax (which means I don't understand, I'm a chicken, and I can only read it roughly). It's mainly because you need to debug C by yourself.

List of basic data types

Because GO supports C language calls, only C and C conversions are listed. As for C codes, it needs to be converted to C language to be called successfully. It should be noted that each C variable is limited to be used in a package. If you want to use it across packages, please use GO to encapsulate it, otherwise you will be prompted for a call error and the C variable cannot be found.

The data type conversion used in the project converts go's string to C

The string of C is a special case of a character array. To put it simply, an array of characters ending with 0 is a string, so it does not belong to the basic data type.

C.CString is the standard library for calling C, and a new memory space has been requested. You need to call C.free to release it, otherwise there will be memory leakage.

Var deviceIp string cdeviceIp: = C.CString (deviceIp) defer C.free (unsafe.Pointer (cdeviceIp)) C char * / char [] converted to go string

Call C's standard library C.GoString, which does not generate new memory space, creates a copy, and does not free up memory space.

The byte array of C to the string of Go

For example, the type of C is: BYTE sSerialNumber [Serialno _ LEN]

The way to get it is to add bytes to the string using append

SerialNo: = make ([] byte, 0) for _, v: = range sSerialNumber {if v! = 0 {serialNo = append (serialNo, byte (v))}}

Note the difference between the character array and the string mentioned earlier.

Go's character array from string to C

Type: CHAR szKeyFilePath [pu _ CERT_FILE_PATH_MAX]

Var keyFilePath = "/ home/docker/path/file.jpg" for I, b: = range keyFilePath {szKeyFilePath [I] = C.CHAR (b)} consortium of data acquisition

When the data of Huawei camera is called back, the union type data is obtained. When it is acquired as an ordinary structure, the compiler will always prompt that the structure cannot be found. Later, after obtaining the data of the union in the C code, convert it to the basic data type, and then call Go again. Post a code snippet and the data obtained by face recognition callback. You don't have to worry about the text before and after, just look at the acquisition of data types.

Void CGopfFaceSnapCallBack (CHAR * szBuffer, LONG lSize, void * pUsrData) {PU_META_DATA * pstMetaData = 0; int ret = Wrapper_IVS_User_GetMetaData (szBuffer, lSize, TARGET, & pstMetaData); if (ret = = PU_FALSE) {return;} PU_UserData * pstMetaUserData = pstMetaData- > pstMetaUserData; char name [100] = {0}; char cardID [100] = {0}; for (UINT uIndex = 0; uIndex)

< pstMetaData->

UsValidNumber; + + uIndex) {/ / printf ("pstMetaData eType:% x\ n", pstMetaUserdata [uIndex] .eType); if (pstMetaUserdata [uIndex] .eType = = FACE_INFO) {strcpy (cardID, pstMetaUserdata [uIndex] .unMetaData.stFaceInfo.cardID); strcpy (name, pstMetaUserdata [uIndex] .unMetaData.stFaceInfo.name); printf ("GopfFaceSnapCallBack unMetaData.stFaceInfo cardID:% s\ n", pstMetaUserdata [uIndex] .unMetaData.stfaceInfo.cardID) Printf ("GopfFaceSnapCallBack unMetaData.stFaceInfo name:% s\ n", pstMetaUserdata [uIndex] .unMetaData.stFaceInfo.name); GopfFaceSnapCallBack (pstMetaUserData.unMetaData.stFaceInfo.cardID, pUsrData); break;}} Wrapper_IVS_User_FreeMetaData (& Index); return;}

If this code is replaced with Go logic and read directly in Go, it will prompt unMetaData that the definition cannot be found. If there are other successful reading methods, please let me know.

The call of callback function of C

1. First, Go code implements functions with consistent data types, and exports them to C functions using / / export. If you find that the callback does not come in, first check whether the data type is correct, and then check whether the trigger conditions are met. The purpose of this step is to receive the callback data from C language in GE language, that is, the callback data is obtained in this function.

2, CGO calls C function, some colleagues said that this step can not be used, directly in the Go call the first step of the function, I have not tried, the company's ancestral code is written in this way, it is directly used.

3. Just call it directly as a common function in the go language.

Look at the code example:

C's function declaration:

Typedef VOID (CALLBACK * pfRealDataCallBack) (CHAR * szBuffer, LONG lSize, VOID * pUsrData)

The code for the first step:

/ / export GopfRealDataCallBackfunc GopfRealDataCallBack (szBuffer * C.CHAR, lSize C.LONG, pUsrData unsafe.Pointer) {fmt.Println (szBuffer,lSize,pUsrData)}

Step 2:

Extern void GopfRealDataCallBack (CHAR * szBuffer, LONG lSize, void * pUsrData); void CGopfRealDataCallBack (CHAR * szBuffer, LONG lSize, void * pUsrData) {return GopfRealDataCallBack (szBuffer,lSize,pUsrData);}

Step 3: C.pfRealDataCallBack (C.CGopfRealDataCallBack), which needs to be declared on import C, otherwise the call will not take effect.

Void* and unsafe.Pointer

Unsafe.Pointer claims to be the transfer bridge for all data types. At the language level, the two can be considered equivalent. When you encounter void*, you can use unsafe.Pointer to receive or transmit it. The conversion of specific types needs to be strongly changed according to the actual type. For example:

LpOutBuff: = unsafe.Pointer (C.malloc (1024))

This 1024 will be revised according to the actual situation, and it is not omnipotent.

Transitive results of the structure array: = (* C.struct_name) (C.malloc (C.size_t (C.sizeof_struct_name * C.int (resLen) defer C.free (unsafe.Pointer (results)

Struct_name was replaced with a specific structure name, applied for space to be released, and GO could not detect the part of C.

Traversing the structure array to get element data for I: = 0; I < int (resLen); iTunes + {result: = (* C.struct_name) (unsafe.Pointer (uintptr (unsafe.Pointer (results)) + uintptr (i*C.sizeof_struct_name)}

Struct_name is replaced with a specific structure name, and uintptr is the memory address of the element, which is obtained according to the offset. Go for i: = 0; I < int (resLen); iTunes + {result: = (* C.DetectFaceResult) (unsafe.Pointer (uintptr (unsafe.Pointer (results)) + uintptr (i*C.sizeof_DetectFaceResult)}

Thank you for reading, the above is the content of "how to use the data conversion commonly used in CGO projects". After the study of this article, I believe you have a deeper understanding of how to use the data conversion commonly used in CGO projects, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report