Недавно добавленные исходники

•  Animation Loaders  807

•  DeLiKaTeS Tetris (Тетрис)  5 773

•  TDictionary Custom Sort  7 729

•  Fast Watermark Sources  7 408

•  3D Designer  10 630

•  Sik Screen Capture  7 944

•  Patch Maker  8 162

•  Айболит (remote control)  8 212

•  ListBox Drag & Drop  7 018

•  Доска для игры Реверси  100 390

•  Графические эффекты  8 286

•  Рисование по маске  7 661

•  Перетаскивание изображений  6 267

•  Canvas Drawing  6 641

•  Рисование Луны  6 564

•  Поворот изображения  5 721

•  Рисование стержней  4 660

•  Paint on Shape  3 362

•  Генератор кроссвордов  4 331

•  Головоломка Paletto  3 489

•  Теорема Монжа об окружностях  4 304

•  Пазл Numbrix  2 783

•  Заборы и коммивояжеры  3 711

•  Игра HIP  2 503

•  Игра Go (Го)  2 495

•  Симулятор лифта  2 890

•  Программа укладки плитки  2 323

•  Генератор лабиринта  3 049

•  Проверка числового ввода  2 554

•  HEX View  2 958

 
скрыть

  Форум  

Delphi FAQ - Часто задаваемые вопросы

| Базы данных | Графика и Игры | Интернет и Сети | Компоненты и Классы | Мультимедиа |
| ОС и Железо | Программа и Интерфейс | Рабочий стол | Синтаксис | Технологии | Файловая система |



Delphi Sources

Как преобразовать указатель на метод в указатель на функцию



// Converting method pointers into function pointers

// Often you need a function pointer for a callback function.
// But what, if you want to specify a method as
// an callback? Converting a method pointer to a function
// pointer is not a trivial task; both types are
// incomatible with each other. Although you have the
// possibility to convert like this "@TClass.SomeMethod",
// this is more a hack than a solution, because it restricts
// the use of this method to some kind of a class
// function, where you cannot access instance variables.
// If you fail to do so, you'll get a wonderful gpf.
// But there is a better solution: run time code generation!
// Just allocate an executeable memory block, and
// write 4 machine code instructions into it: 2 instructions
// loads the two pointers of the method pointer
// (code & data) into the registers, one calls the method
// via the code pointer, and the last is just a return
// Now you can use this pointer to the allocated memory
// as a plain funtion pointer, but in fact you are
// calling a method for a specific instance of a Class.

type
  TMyMethod = procedure of object;

function MakeProcInstance(M: TMethod): Pointer;
begin
  // allocate memory
  GetMem(Result, 15);
  asm
    // MOV ECX,
    MOV BYTE PTR [EAX], $B9
    MOV ECX, M.Data
    MOV DWORD PTR [EAX+$1], ECX
    // POP EDX
    MOV BYTE PTR [EAX+$5], $5A
    // PUSH ECX
    MOV BYTE PTR [EAX+$6], $51
    // PUSH EDX
    MOV BYTE PTR [EAX+$7], $52
    // MOV ECX,
    MOV BYTE PTR [EAX+$8], $B9
    MOV ECX, M.Code
    MOV DWORD PTR [EAX+$9], ECX
    // JMP ECX
    MOV BYTE PTR [EAX+$D], $FF
    MOV BYTE PTR [EAX+$E], $E1
  end;
end;

procedure FreeProcInstance(ProcInstance: Pointer);
begin
  // free memory
  FreeMem(ProcInstance, 15);
end;

// After all, you should not forget to release the allocated memory. 
// "TMyMethod" can be modified according your specific needs, e.g. 
add some parameters for a WindowProc. 
// N.B.: Yes, I know, Delphi has those "MakeProcInstance" 
function in its forms unit. 
// But this works a little bit different, has much more overhead, 
// and most important, you have to use the forms unit, which increases 
the size of your exe drastically, 
// if all other code doesn't use the VCL 
(e.g. in a fullscreen DirectX/OpenGl app). 




Похожие по теме исходники

Сортировка методом Хоара

Метод Рунге-Кутта решения дифур

Метод Симпсона




Copyright © 2004-2026 "Delphi Sources" by «SiteAnalyzer». Delphi World FAQ

Группа ВКонтакте