flat assembler
Message board for the users of flat assembler.

Index > Windows > Call func from DLL written at Golang.

Author
Thread Post new topic Reply to topic
greendruid



Joined: 15 Jan 2021
Posts: 3
greendruid 15 Jan 2021, 14:45
Hello!
I faced with the trouble to passing parameters to the function, called from DLL written at Golang. I try send 'string' ('db'), but the 'function-receiver' show a unclear junk. I think, that is data's type problem. In the similar code at 'c' has been used the next construction:
Code:
GoString tstr = lpFunc("test");
char* cTstr = malloc(tstr.n + 1);
if (!cTstr) 
    memcpy(cTstr, tstr.p, tstr.n);
cTstr[tstr.n] = '\0';
printf("Message from DLL: %s\n", tStr);
    

Also 'GoString' has been defined in 'h' file:
Code:
#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif
    

Should I focused at Golang's feature for find a solution? Or I could to prepare a data special in fasm before sending to the function? In the future I'll need to pass 'byte array' to another function.

FASM source:
Code:
format PE64 console
entry start
include 'c:\fasm\include\win64a.inc'
section '.text' code readable executable
start:
        sub     rsp,8
        invoke  PrintHello,[message]
        invoke  ExitProcess,0
section '.data' data readable
  message db "test data",0
section '.idata' import data readable writeable
  library kernel32,'KERNEL32.DLL',\
          writemsg,'goDLL.dll'
  include 'c:/fasm/include/api/kernel32.inc'
  import writemsg,\
         PrintHello,'PrintHello'
    


Part src at Golang:
Code:
//export PrintHello
func PrintHello(s *C.char)(r string){
        t := C.GoString((*C.char)(unsafe.Pointer(s)))
        //fmt.Printf("Test message %v\n", len(t))
        //fmt.Println("Test message %v", t)
        return "test from dll";
}
    


Thank you!
Post 15 Jan 2021, 14:45
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 802
Location: Russian Federation, Sochi
ProMiNick 15 Jan 2021, 15:37
greendruid wrote:
Code:
        invoke  PrintHello,[message]    
my eyes crying, why not this:
Code:
        invoke  PrintHello,message    

_________________
I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.
Post 15 Jan 2021, 15:37
View user's profile Send private message Send e-mail Reply with quote
greendruid



Joined: 15 Jan 2021
Posts: 3
greendruid 15 Jan 2021, 16:24
ProMiNick wrote:
greendruid wrote:
Code:
        invoke  PrintHello,[message]    
my eyes crying, why not this:
Code:
        invoke  PrintHello,message    


Hi, thanks for you attention! My mistake (copy the one of draft). But no success if I changing to a correct version (without square brackets).
Post 15 Jan 2021, 16:24
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 802
Location: Russian Federation, Sochi
ProMiNick 16 Jan 2021, 04:10
Code:
section '.data' data readable
  dd -1
  message db "test data",0    

maybe string format:
Code:
length dword ; struct negate offset
string char[] ; struct zero offset ; zero terminated    
Post 16 Jan 2021, 04:10
View user's profile Send private message Send e-mail Reply with quote
greendruid



Joined: 15 Jan 2021
Posts: 3
greendruid 17 Jan 2021, 10:08
ProMiNick wrote:

Code:
length dword ; struct negate offset
string char[] ; struct zero offset ; zero terminated    

No success. Output in terminal looks like:
Code:
�☼H�� �
    

I can to fix something in the DLL. Maybe I should change a data type to '[]byte' (byte array) in Golang library and pass a data as bytes (fix in fasm src)?
Post 17 Jan 2021, 10:08
View user's profile Send private message Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 719
Ali.Z 17 Jan 2021, 23:13
i would say you better double check the reference manual for that specific function, see what arguments it takes and what calling conventions it use.

_________________
Asm For Wise Humans
Post 17 Jan 2021, 23:13
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.