News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Translating Invoke With GetProcAddress

Started by DeadlyVermilion, August 22, 2010, 02:08:51 PM

Previous topic - Next topic

bomz

I don't well understand what are talking about
http://forum.codenet.ru/showthread.php?p=172896
This topic refers to the fact that MSDN GetProcAddress article have mistake.

Twister


bomz

http://translate.google.ru/?hl=ru#
I can't help you quickly, because I don't understand sence of C code. I trying. Reading just now
In this code authors correct mistake
Quote
typedef  HRESULT (__stdcall*MYPROC)(LPUNKNOWN, LPCSTR, LPCSTR, DWORD, LPBINDSTATUSCALLBACK);
int main(int argc, char* argv[])
{
    MYPROC ProcAdd(NULL);
    HMODULE hLib = LoadLibrary(TEXT("urlmon.dll"));
    HRESULT hr (0);
    if (hLib)
    {
        ProcAdd = (MYPROC) GetProcAddress(hLib, "URLDownloadToFileA");
        if (ProcAdd) hr = ProcAdd(NULL, "http://www.google.com", "C:\\123.htm", 0, NULL);
        FreeLibrary(hLib);
    }   
    return hr;
}

Quote
not just the correct signature function that is referenced by a pointer and therefore you had an error stack pointer
????????????

Twister

All that does it get the address to "URLDownloadToFile" and calls it. I'm not sure what you don't get from that small piece of code.

Yuri

In that thread, nobody is talking about an MSDN error. The problem was first the wrong number of parameters for URLDownloadToFile and then the wrong calling convention for it (cdecl instead of stdcall).

donkey

Quote from: Yuri on August 23, 2010, 01:56:12 AM
In that thread, nobody is talking about an MSDN error. The problem was first the wrong number of parameters for URLDownloadToFile and then the wrong calling convention for it (cdecl instead of stdcall).

Not sure about the calling convention, it is listed as STDAPI in the headers which resolves to STDCALL, but having tested the function in a quick and dirty way:

PrintDec(esp)
invoke urlmon.dll:URLDownloadToFileA,0,0,0,0,0
PrintDec(esp)

it yeilds:

Line 29: esp = 1245020
Line 31: esp = 1245020

Which indicates that it is a STDCALL function and that it does have 5 parameters as indicated at MSDN and in urlmon.h

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Yuri

Quote from: donkey on August 23, 2010, 02:33:12 AM
Which indicates that it is a STDCALL function

Yes, exactly, but in that topic it was at first declared as cdecl. In fact, no convention was specified, but in C and C++ it defaults to cdecl, as far as I know.

donkey

Quote from: Yuri on August 23, 2010, 03:39:54 AM
Quote from: donkey on August 23, 2010, 02:33:12 AM
Which indicates that it is a STDCALL function

Yes, exactly, but in that topic it was at first declared as cdecl. In fact, no convention was specified, but in C and C++ it defaults to cdecl, as far as I know.

Not sure how it first appeared but the only docs I have show it returns an HRESULT which by definition is StdCall.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Yuri

Edgar, you are getting me wrong. What I am talking about is not MSDN or other MS docs, but the link that bomz gave a few posts ago: http://forum.codenet.ru/showthread.php?p=172896. There the topic starter first declared a cdecl function pointer with one parameter, but later assigned it to a pointer to URLDownloadToFile. Which, of course, is stdcall and takes 5 parameters, you are absolutely right about that. That is the creator of that topic on the Russian forum who was wrong, and also bomz when he wrote here:
Quote
This topic refers to the fact that MSDN GetProcAddress article have mistake.
Nobody in that topic says that the GetProcAddress article on MSDN has any mistakes.

donkey

"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

bomz

that article use frase something like that: msdn don't think about. I don't know how to translate it in english. "error" was bad translation

inaccuracy

this article and that article begin from the same frase
Hey there people, I'm trying to learn how to call API's Dynamically to reduce the amount I have declared.
Quote
Добрый день не могу разобрасться с ф-ей GetProcAddress делаю как в примере MSDN ничего не помогает вылазит ошибка:
http://translate.google.ru
Good afternoon, I can not deal with the function GetProcAddress do as in the example MSDN nothing helps climbs error:

Yuri

Quote from: bomz on August 23, 2010, 10:48:42 AM
that article use frase something like that: msdn don't think about. I don't know how to translate it in english. "error" was bad translation

Случайно не это имеете в виду?
Quote
ох не досмотрел MSDN
Это автор фразы не досмотрел как следует пример в MSDN. Поскольку в примере соглашение вызова указано (WINAPI == STDCALL):

typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);


bomz

ага. вот это. ну еще смешнее получилось.

hutch--

Guys,

Tolerate me here, what does the content of an external site have to do with the API functions LoadLibrary(), GetProcAddress(), FreeLibrary() ?

The documentation and usage of these functions have been known for years and YES they are STDCALL.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Vortex

The application below without importing any function finds the address of kernel32.dll :


.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc

szCmp       PROTO :DWORD,:DWORD
GetProcAddr PROTO :DWORD,:DWORD

prC         TYPEDEF PROTO C :VARARG
LoadLibrary EQU <pr1 PTR LoadLib>
printf      EQU <prC PTR __printf>

.data

LLibrary        db "LoadLibraryA",0
FreeLibrary     db "FreeLibrary",0
ExitProcess     db "ExitProcess",0
_printf         db "printf",0
msvcrt          db "msvcrt.dll",0
msg             db 'Kernel address = %X',0

LoadLib         db 0FFh,025h ; define a jump entry
                dd pLoadLib


__printf        db 0FFh,025h
                dd pprintf


.data?

pLoadLib        dd ?
pprintf         dd ?
hLib            dd ?
hKernel32       dd ?

.code

start:

; The code to get the base of kernel32
; may not work on every version of Windows

    mov     ecx,[esp]

@@:

    xor     edx,edx
    dec     ecx
    mov     dx,[ecx+03ch]
    test    dx,0f800h
    jnz     @b
    cmp     ecx,[ecx+edx+34h]
    jnz     @b

    mov     hKernel32,ecx

    invoke  GetProcAddr,hKernel32,ADDR LLibrary
    mov     pLoadLib,eax

    invoke  LoadLibrary,ADDR msvcrt
    mov     hLib,eax

    invoke  GetProcAddr,eax,ADDR _printf
    mov     pprintf,eax
   
    invoke  printf,ADDR msg,hKernel32

    invoke  GetProcAddr,hKernel32,ADDR FreeLibrary
    push    hLib
    call    eax
   
    invoke  GetProcAddr,hKernel32,ADDR ExitProcess

    push    0
    call    eax

include     GetProcAddr.inc
include     szCmp.inc

END start