Ian:

Thanks.

In the real environment, t transform the error message into a JSON document 
and C.free as soon as the transformation is done.  The end user will see 
the proverbial error interface.

With regards,
mp

On Friday, December 8, 2017 at 10:24:45 AM UTC-7, M.P. Ardhanareeswaran 
wrote:
>
> All:
>
> I am able to pass a []*C.SQL_ERROR_TYPE to C, allocate memory for 
> SQL_ERROR_TYPE on the "C" side, populate the structure and access it on the 
> Go side.  It works.  I tested Mac (High Sierra) and Linux 7, 64 bit.  Is 
> this legitimate?  Or is it just happenstance that it is working?
>
> With regards,
>
> package ttodbc
>
> import "unsafe"
> import "fmt"
>
> /*
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> typedef struct sqlError {
>   int recordNumber;
>   char sqlState[32];
>   int nativeError;
>   char messageText[1024];
> } SQL_ERROR_TYPE;
>
> void *CSlice(void* slice) {
>   int pos=0;
>   SQL_ERROR_TYPE **tmp = (SQL_ERROR_TYPE **) slice;
>   void *dummy;
>   for (pos =0; pos < 16; pos++) {
>     printf("Populating %d\n", pos);
>     tmp[pos] = malloc(sizeof(SQL_ERROR_TYPE));
>     tmp[pos]->nativeError = pos+100;
>     tmp[pos]->recordNumber = pos;
>     strcpy(tmp[pos]->sqlState, "S1000");
>   }
>   dummy = malloc(sizeof(int) );
>   return dummy;
> }
> */
> import "C"
>
> func SliceToC() {
> slice := make([]*C.SQL_ERROR_TYPE, 16)
> ret := C.CSlice(unsafe.Pointer(&slice[0]))
> fmt.Println("Ret %v", ret)
> for i := 0; i < 16; i++ {
> var tmp *C.SQL_ERROR_TYPE = slice[i]
> fmt.Println("SQLState, native error: ", C.GoString(&tmp.sqlState[0]), 
> uint(tmp.nativeError))
> }
> }
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to