Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9265993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:12:50+00:00 2026-06-18T14:12:50+00:00

Problem I have a function bob written in assembler (nasm), which makes use of

  • 0

Problem

I have a function “bob” written in assembler (nasm), which makes use of functions in kernel32.dll. And I have a program in FreePascal, that calls “bob”.

I use nasm with:

nasm -fwin32 bob.asm

In FreePascal I declare:

{$link bob.obj}

function bob(s:pchar):longint; stdcall; external name 'bob';

But I get an error when I compile with fpc, telling it doesn’t find GetStdHandle and WriteConsoleA (without @n suffix), which are declared extern in bob.asm. I would like to tell fpc to look for them in kernel32.dll, or in an adequate import library.

However, when I use the same function in pure assembly program, it works fine with nasm and golink. And when I don’t call DLL functions, I can link with FreePascal with no trouble.

How can I link kernel32 functions with FreePascal, so that assembly functions “see” them ?


A Solution

Given by BeniBela. I change names so that things are easy to follow.

program dlltest;

function WindowsGetStdHandle(n: longint): longint; stdcall;
   external 'kernel32.dll' name 'GetStdHandle';

{$asmmode intel}
procedure WrapperGetStdHandle; assembler; public name 'AliasGetStdHandle';
asm
   jmp WindowsGetStdHandle
end;


{$link myget.obj}

function AsmGetStdHandle(n: longint): longint; stdcall;
   external name 'gethandle';

const STDOUT = -11;

begin
   writeln(AsmGetStdHandle(STDOUT));
   writeln(WindowsGetStdHandle(STDOUT));
end.

With this in assembly, in myget.asm:

section .text

extern AliasGetStdHandle

global gethandle

gethandle:
   mov   eax, [esp+4]
   push  eax
   call  AliasGetStdHandle
   ret   4

WindowsGetStdHandle is another name for GetStdHandle in kernel32.dll.

WrapperGetStdHandle only jump to the preceding, it’s here for the alias or public name capability : we give it the name AliasGetStdHandle for external objects. This is the important part, the function get visible to the assembly program.

AsmGetStdHandle is the name in FreePascal of the assembly function gethandle. It calls WrapperStdHandle (nicknamed AliasGetStdHandle), which jumps to WindowsGetStdHandle, the DLL function.

And we are done, now the assembly program can be linked, without changing anything in it. All the renaming machinery is done in the pascal program calling it.

The only drawback: the need for a wrapper function, but it’s not overpriced for a fine control of names.


Another solution

If kernel32.dll is not specified in declaration of WindowsGetStdHandle, but with {$linklib kernel32}, then the symbol gets visible in object files linked in the pascal program. However, it seems the $linklib directive alone is not enough, one still has to declare in pascal some function refering to it

program dlltest;

{$linklib kernel32}

function WindowsGetStdHandle(n: longint): longint; stdcall;
   external name 'GetStdHandle';

{$link myget.obj}

function AsmGetStdHandle(n: longint): longint; stdcall;
   external name 'gethandle';

const STDOUT = -11;

begin
   writeln(AsmGetStdHandle(STDOUT));
   writeln(WindowsGetStdHandle(STDOUT));
end.

With the following assembly program. AliasGetStdHandle is replaced with GetStdHandle, which now points directly to kernel32 function.

section .text

extern GetStdHandle

global gethandle

gethandle:
         mov   eax, [esp+4]
         push  eax
         call  GetStdHandle
         ret   4

But this only works when using the external linker (gnu ld), with command

fpc -Xe dlltest.pas

When omitting opton ‘-Xe’, fpc gives the following error

Free Pascal Compiler version 2.6.0 [2011/12/25] for i386
Copyright (c) 1993-2011 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling dlltest.pas
Linking dlltest.exe
dlltest.pas(17,1) Error: Asm: Duplicate label __imp_dir_kernel32.dll
dlltest.pas(17,1) Error: Asm: Duplicate label __imp_names_kernel32.dll
dlltest.pas(17,1) Error: Asm: Duplicate label __imp_fixup_kernel32.dll
dlltest.pas(17,1) Error: Asm: Duplicate label __imp_dll_kernel32.dll
dlltest.pas(17,1) Error: Asm: Duplicate label __imp_names_end_kernel32.dll
dlltest.pas(17,1) Error: Asm: Duplicate label __imp_fixup_end_kernel32.dll
dlltest.pas(17,1) Fatal: There were 6 errors compiling module, stopping
Fatal: Compilation aborted
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:12:51+00:00Added an answer on June 18, 2026 at 2:12 pm

    I do not know how to fix the linking issue directly, but you could declare public wrapper functions that export these functions from the Pascal source.

    E.g.:

    {$ASMMODE INTEL}
    procedure WrapperGetStdHandle; assembler; public; alias: '_GetStdHandle@4';
    asm  jmp GetStdHandle end;
    procedure WrapperWriteConsoleA; assembler; public; alias: '_WriteConsoleA@20';
    asm  jmp WriteConsoleA end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with function checking collision. No matter what I do it throws
i have function to send mail with attachment to microsoft exchange server. My problem
I have a problem with my BroadcastReceiver. Basically, I have a function in my
Problem I have computed a probability density function that depends on two variables. I
I have problem with passing an argument to my simple function in jQuery: When
I have this function above to create url slugs from posts title, the problem
I have a function that will parse some data coming in. My problem is
I have problem with fancybox. I want to write a function that will run
In PHP I have a function, the problem is it will output an extra
I've a problem with celery's logger. I have a function that renders frames. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.