flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Correctly calling dll proc from c#, return value issues

Author
Thread Post new topic Reply to topic
jmurray



Joined: 28 Sep 2019
Posts: 8
Location: Plymouth, UK
jmurray 01 Oct 2019, 15:17
Hello,
After a lot of trying different things I feel like i'm pretty close to getting this working.

I'm calling MethodOneID and expecting back 6 dd double word values. But from c# I get the error "Cannot marshal 'return value': Invalid managed/unmanaged type combination."

My dll code is:
Code:
proc MethodOneID LFirst, HFirst, LSec, HSec, LLast, HLast
     ;measure timestamp
     rdtsc
     mov [LFirst],eax
     mov [HFirst],edx
     MFENCE
     ;measure timestamp
     rdtsc
     mov [LSec],eax
     mov [HSec],edx
     MFENCE
     ;Code to measure
     CPUID
     ;measure timestamp
     rdtsc
     mov [LLast],eax
     mov [HLast],edx
     MFENCE
     ret
endp
    

And my C# code calling the procedure is:
Code:
        [DllImport("Test-DLL.DLL", EntryPoint = "MethodOneID")]
        static extern UInt32[] MethodOneID();
    


I'm wondering how to correctly receive the data from the dll, or whether I need to format the procedure differently somehow.

Thank you for any help/guidance. It is very much appreciated.
Post 01 Oct 2019, 15:17
View user's profile Send private message Send e-mail Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 02 Oct 2019, 22:44
You have to allocate that array somewhere. Either pass an Int32 array/structure to the function or have the function allocate the memory for you, which means you'll also have to have the DLL free the memory.

If you are feeling adventurous you can even go to the extreme and use shared memory, PIPES, and even sockets.
Post 02 Oct 2019, 22:44
View user's profile Send private message Reply with quote
juergenkulow



Joined: 05 Oct 2019
Posts: 1
juergenkulow 05 Oct 2019, 13:27
Hello jmurray,

Code:
using System;
using System.Runtime.InteropServices;
namespace TestDLL{
    class Program    {
        [DllImport("D:\\Test-DLL.dll" , EntryPoint= "MethodOneID")]
        public static extern int MethodOneID([In,Out] long []  regcx);
        private static long[] buffer = new long[] { 1, 2, 3 };
        static void Main(string[] args) {
            MethodOneID( buffer); 
            Console.WriteLine(buffer[0]);
            Console.WriteLine(buffer[1]);
            Console.WriteLine(buffer[2]);
            Console.ReadLine();
        }
    }
}
    

Code:
format PE64 console DLL
entry DllEntryPoint

include 'win64a.inc'

section '.text' code readable executable

proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
        mov     eax,TRUE
        ret
endp

proc MethodOne uses rcx
     rdtsc
     mov [rcx],eax
     add rcx,4
     mov [rcx],edx
     add rcx,4
     MFENCE
     ;measure timestamp
     rdtsc
     mov [rcx],eax
     add rcx,4
     mov [rcx],edx
     add rcx,4
     push rcx ; CPUID delete rcx
     MFENCE
     ;Code to measure
     CPUID
     ;measure timestamp
     rdtsc
     pop rcx
     mov [rcx],eax
     add rcx,4
     mov [rcx],edx
     add rcx,4
     MFENCE
     Mov rax,rcx
     ret
endp

section '.bss' data readable writeable

  bytes_count dd ?

section '.edata' export data readable

  export 'Test-DLL.DLL',\
          MethodOne,'MethodOneID'

section '.reloc' fixups data readable discardable

  if $=$$
    dd 0,8              ; if there are no fixups, generate dummy entry
  end if


    
Post 05 Oct 2019, 13:27
View user's profile Send private message Reply with quote
jmurray



Joined: 28 Sep 2019
Posts: 8
Location: Plymouth, UK
jmurray 05 Oct 2019, 19:14
Fantastic. Thanks juergenkulow. That works and clears up a lot if my misconceptions. I should be alright from here on out.
Post 05 Oct 2019, 19:14
View user's profile Send private message Send e-mail 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.