I have the next case:
There is an application (presumably in C/C ++) that runs on Windows (32 bit) and pulls in a bunch of .dll files. We do not have access to the source code for this application. The task is to write a small driver in the image and likeness of the old .dll, but in Go.
Work flow and the problem: a string of type char* is passed to the Go function from this application, and we need to write some result string by this pointer. As we were told, the memory has already been allocated, i.e. on the Go side, we don't need to do any malloc, just write the result. As a result, an Access violation occurs with the value of the pointer on which we are trying to write.
Does anyone know anything about this?
We wrote a similar shared library in C ++ and assembled everything as a .dll, which also writes the result to the string-input parameter, the result: everything works as expected.
We logged input pointers both, in Go and C++ shared librarys, and they are different. We guess it may be some kind of shift in the areas of memory when pulling in Go and C++ .dll files to main app.
I also wrote a simple app in C that calls .dll and everything worked just fine, Go library was able to write result by pointer that it have received from C app. The difference is that in this case the linking took place at the compilation stage, but on the production it occurs at runtime. So at this point we don't have any ideas.
UPD: My Go code of Test function (doesn't work)
//export Test
func Test(ptrParams *C.char, count C.int, resultStr *C.char) int {
log.Info("Test func exec start ...")
str := "test_driver_success"
C.strcpy((*C.char)(resultStr), (*C.char)(C.CString(str)))
log.Info("Test func exec end ...")
return 0
}
Error: Access violation at address 755EF448 in module 'msvcrt.dll'. Write of address 0051B3B1
C++ code that works
EXTERN_DLL_EXPORT int Test(char *params, int params_count, char* result)
{
strncpy_s(result, 100, "test_driver_success", strlen("test_driver_success")+1);
}