Friday, April 26, 2024
HomeGolangCgo compiled c-shared binary string returned as unusual encoding from C++ compiled...

Cgo compiled c-shared binary string returned as unusual encoding from C++ compiled with Ubuntu – Getting Assist


I’ve a small quantity of go code that I compile utilizing cgo as a c-shared binary. This code is then later utilized in a C++ file that’s compiled utilizing gcc with Ubuntu.

Now after I use this identical code however compile on M1 Mac every thing is okay and the perform’s returned string in C++ seems regular. Nonetheless after I compile it in Ubuntu, once more identical code, the features run with out error however the perform’s returned string is that this unusual garbled encoding. It seems like this but it surely differs on every run. I don’t assume its encryption as a result of the return string inside the C code is regular.

// C

func GetOsStore(serviceName *C.char, keyName *C.char) *C.char {
	fmt.Println("GetOsStore begin")
	backendType, err := getBackendType()
	if err != nil {
		panic("Getting backend kind has failed! " + err.Error())
	}
	fmt.Println("GetOsStore.BackendType", backendType)

	ring, openErr := keyring.Open(keyring.Config{
		AllowedBackends: []keyring.BackendType{backendType},
		ServiceName:     C.GoString(serviceName),
	})
	if openErr != nil {
		fmt.Println("GetOsStore open keyring error", openErr)
	} else {
		fmt.Println("GetOsStore opened keyring")
	}

	i, getErr := ring.Get((C.GoString(keyName)))
	dataStr := string(i.Information[:])
	returnStr := (*C.char)(C.CString(dataStr)) // returnStr prints with out subject
	defer C.free(unsafe.Pointer(returnStr))
	if getErr != nil {
		fmt.Println("GetOsStore get keyring error", getErr)
	} else {
		fmt.Println("GetOsStore bought keyring")
	}
	fmt.Println("GetOsStore finish")
	return returnStr
}

// C++

Napi::String getOsStore(const Napi::CallbackInfo& information) {
  Napi::Env env = information.Env();
  
  std::string serviceNameArg = information[0].As<Napi::String>().ToString();
  char *serviceName = new char[serviceNameArg.length() + 1];
  strcpy(serviceName, serviceNameArg.c_str());

  std::string keyNameArg = information[1].As<Napi::String>().ToString();
  char *keyName = new char[keyNameArg.length() + 1];
  strcpy(keyName, keyNameArg.c_str());

  Napi::String end result = Napi::String::New(env, GetOsStore(serviceName, keyName));
  std::string resultStr = end result.ToString();
  cout << "getOsStore from c++ string: " << resultStr; // each these cout produce the encoded textual content
  char *resultCStr = new char[resultStr.length() + 1];
  strcpy(resultCStr, resultStr.c_str());
  cout << "getOsStore from c string: " << resultCStr; 

  delete [] serviceName;
  delete [] keyName;
  return end result;
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments