Click here to Skip to main content
16,005,121 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: error LNK2001 Pin
sunit518-Aug-05 18:56
sunit518-Aug-05 18:56 
Generalyeah! so true! Pin
valerie9919-Aug-05 4:12
valerie9919-Aug-05 4:12 
Generalquestion related to drivers Pin
gamitech18-Aug-05 10:09
gamitech18-Aug-05 10:09 
General__stdcall Pin
Anton Mikhalyov18-Aug-05 8:09
Anton Mikhalyov18-Aug-05 8:09 
GeneralRe: __stdcall Pin
Chris Losinger18-Aug-05 8:33
professionalChris Losinger18-Aug-05 8:33 
GeneralRe: __stdcall Pin
Anton Mikhalyov18-Aug-05 17:55
Anton Mikhalyov18-Aug-05 17:55 
GeneralRe: __stdcall Pin
MailtoGops19-Aug-05 4:14
MailtoGops19-Aug-05 4:14 
GeneralRe: __stdcall Pin
Anton Mikhalyov19-Aug-05 7:06
Anton Mikhalyov19-Aug-05 7:06 
>There is a flaw in your code. Thats why it is crashing. Basically compilier try to do second stack clean up.. .
I know that compiler do stack cleaning after calling this function and in my disassembly I marked this place with comment.

>>typedef HANDLE (__stdcall *FOO)(...); // function must be called via std convention

>The above declaration is the problem.. As is GetModuleHandleA is a standard function following pascal calling (Standard windows) convention.
This is not true. Pascal convention isn't standart windows dll's convention! Standart Windows convention is exacly STD.
If you don't trust me just try this code:

#include <stdio.h>
#include <windows.h>

void main()
{
const char* src = "Let's test";
USHORT srcSz = strlen(src);
char *dst = new char[srcSz+1];
memset(dst, 0x00, sizeof(src)+1);
void *lpLstrcpy = GetProcAddress(LoadLibrary("kernel32.dll"), "lstrcpy");

printf("Testing STD Calling Convention...\n"
"In this convention parameters must be stored in stack in back order\n"
"And function must clean all it's parameters from stack by itself\n");

__asm {
; I put in stack all parameters in flip order(STD convention rule)
push src ; lstrcpy(DESTINATION, SOURCE);
push dst ; ^ |
call lpLstrcpy ; ----------+
}
printf("In dst variable now : %s\n", dst);
memset(dst, 0x00, srcSz+1); // Zero memory at dst
printf("\nTesting Pascal Calling Convention...\n"
"In this convention parameters must be put in stack in normal order\n"
"And function doesn't cleans all it's parameters from stack by itself\n");

__try {
__asm {
push dst
push src
call lpLstrcpy
; Clean stack
sub esp, 0x08
}
} __except (EXCEPTION_EXECUTE_HANDLER) {
printf("\nOops!\n\n");
}
printf("In dst variable now - %s\nOf course you don't see anything after '-'\n", dst);
system("PAUSE");
}

>In Pascal calling convention, the number of paramter we are passing to the function and the number of bytes required for that must be known before calling a function.
>By that way the called function will do clean up.
Smile | :) If function want it can clean stack if don't can not.
Conventions is a way to describe how compiler must generate it's code to call some function.

>You have declared a function pointer like this..
>typedef HANDLE (__stdcall *FOO)(...);
>(...) which means the function would accept many number of parameters. C/CPP compiler would consider this this is a "C" calling convention. You are calling function in the dll.. That is following pascal calling convention..
I know that (...) means that to function will be passed unknown number of parameters.
By UNIX way (...) must be replaced with 'va_list'.

>When you call the function, the called function will unwind the stack as of pascal calling convention. And your calling function will also try to empty the non-existing stack(Already cleaned).. Thus a problem..
>See my uncorrupt() function against your function.
>void nocorrupt()
>{
>typedef HANDLE (__stdcall *FOO)(LPCTSTR); // function must be called via std convention
>FOO foo = (FOO)GetProcAddress(LoadLibrary("kernel32.dll"), "GetModuleHandleA");
>(*foo)(NULL);
>}
>This won't crash..
I don't know how many parameters will be passed to function, but in your code we have a fixed number of parameters - 1.
GeneralRe: __stdcall Pin
MailtoGops22-Aug-05 5:40
MailtoGops22-Aug-05 5:40 
GeneralAutomation: How do I select and copy text from a word document Pin
sammiantha18-Aug-05 7:31
sammiantha18-Aug-05 7:31 
GeneralRe: Automation: How do I select and copy text from a word document Pin
David Crow18-Aug-05 7:58
David Crow18-Aug-05 7:58 
GeneralRe: Automation: How do I select and copy text from a word document Pin
gamitech18-Aug-05 9:50
gamitech18-Aug-05 9:50 
GeneralRe: Automation: How do I select and copy text from a word document Pin
Garth J Lancaster18-Aug-05 19:20
professionalGarth J Lancaster18-Aug-05 19:20 
GeneralRe: Automation: How do I select and copy text from a word document Pin
sammiantha20-Aug-05 7:57
sammiantha20-Aug-05 7:57 
GeneralRe: Automation: How do I select and copy text from a word document Pin
sammiantha20-Aug-05 10:59
sammiantha20-Aug-05 10:59 
GeneralRe: Automation: How do I select and copy text from a word document Pin
Garth J Lancaster20-Aug-05 17:06
professionalGarth J Lancaster20-Aug-05 17:06 
GeneralRe: Automation: How do I select and copy text from a word document Pin
sammiantha21-Aug-05 21:00
sammiantha21-Aug-05 21:00 
GeneralRe: Automation: How do I select and copy text from a word document Pin
Garth J Lancaster21-Aug-05 21:11
professionalGarth J Lancaster21-Aug-05 21:11 
Generalconverting ebcdic to ascii Pin
Camron18-Aug-05 7:30
Camron18-Aug-05 7:30 
GeneralRe: converting ebcdic to ascii Pin
BlackDice18-Aug-05 7:38
BlackDice18-Aug-05 7:38 
GeneralRe: converting ebcdic to ascii Pin
David Crow18-Aug-05 7:51
David Crow18-Aug-05 7:51 
Generalidentifying disk drives and media type Pin
Chintoo72318-Aug-05 7:14
Chintoo72318-Aug-05 7:14 
GeneralRe: identifying disk drives and media type Pin
Ravi Bhavnani18-Aug-05 7:35
professionalRavi Bhavnani18-Aug-05 7:35 
GeneralRe: identifying disk drives and media type Pin
Chintoo72318-Aug-05 7:56
Chintoo72318-Aug-05 7:56 
GeneralRe: identifying disk drives and media type Pin
Ravi Bhavnani18-Aug-05 8:13
professionalRavi Bhavnani18-Aug-05 8:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.