2

P/Invoke を使用するときに独自の呼び出し規約を指定できるようにする必要があるビジネス ケースがあります。具体的には、非標準の ABI を使用する従来の dll があり、関数ごとに呼び出し規約を指定できるようにする必要があります。

たとえば、この dll の 1 つの関数は、最初の 2 つの引数を EAX と EBX 経由で受け取り、残りはスタック経由で受け取ります。別の関数は、ECX を介して 1 つの引数を受け取り、残りはスタックに置きます。これらの関数は数百ありますが、これらの関数にアクセスするために独自の中間ブリッジ DLL を作成することは避けたいと考えています。

私のもう 1 つのオプションは、独自のカスタム P/Invoke を手動でロールすることですが、これは明らかな理由から望ましくありません。

どんな助けでも大歓迎です、ありがとう、

4

3 に答える 3

3

カスタム P/Invoke の意味がわかりませんが、インライン アセンブリを使用した非マネージド C++ なしではどうやって逃げることができるのかわかりません。ただし、ほとんどすべてが 32 ビット値として渡されるため、関数ごとに 1 つではなく、関数シグネチャごとに 1 つのプロキシのみを記述しても問題ありません。または、XML からプロキシを生成するコード ジェネレーターを作成することもできます。ただし、すべてのプロキシ関数は非常にシンプルになるため、このバージョンがあまり望ましくないとは思えません。

int RealFunction(int param1, const char * param2, char param 3);

int MyFunction(int param1, int param2, int param3) { // argument types do not matter as long as they are not doubles or structures
   __asm {
      mov eax, param1
      mov ebx, param2
      push param3
      call RealFunction
      ; depending on calling convention, you might need to do add esp, 12 here
      ; if RealFunction does not return its result in eax, you will need to do mov eax, <wherever the return value is> here
   }
}
于 2008-12-26T13:26:05.713 に答える
0

私は、別のdllなしであなたが望むことを達成する組み込みの方法がないことはかなり確信しています. ランタイム システムがサポートする以外の呼び出し規約を指定する方法は見たことがありません。

于 2008-12-24T18:29:21.827 に答える
0

しばらく前に呼び出し規約について学んでいて、呼び出し規約を変換するコードを書きました。コードは特別な C# ラッパーから呼び出され、ラッパー ライブラリはリフレクション エミット (Marshal.getdelegateforfunctionpointer を動作させることができませんでした) を使用して、特別なネイキッド スタブ メソッドの新しい p/invoke メソッドをエミットします。パラメータを修正してから、実際のメソッドを呼び出します。

ここにcコードがあります。私はC#の部分を手元に持っていません:(当時私もアセンブラを学んでいたので、コードはうまくいかないかもしれません:)

typedef struct
{
    USHORT ParameterOneOffset;  // The offset of the first parameter in dwords starting at one
    USHORT ParameterTwoOffset;  // The offset of the second parmaeter in dwords starting at one
} FastCallParameterInfo;



    __declspec( naked,dllexport ) void __stdcall InvokeFast()
{
    FastCallParameterInfo paramInfo;
    int functionAddress;
    int retAddress;
    int paramOne, paramTwo;
    __asm
    {
        // Pop the return address and parameter info.  Store in memory.
        pop retAddress;
        pop paramInfo;
        pop functionAddress;

        // Check if any parameters should be stored in edx                          
        movzx ecx, paramInfo.ParameterOneOffset;     
        cmp ecx,0;
        je NoRegister;  

        // Calculate the offset for parameter one.
        movzx ecx, paramInfo.ParameterOneOffset;    // Move the parameter one offset to ecx
        dec ecx;                                    // Decrement by 1
        mov eax, 4;                                 // Put 4 in eax
        mul ecx;                                    // Multiple offset by 4

        // Copy the value from the stack on to the register.
        mov ecx, esp;                               // Move the stack pointer to ecx
        add ecx, eax;                               // Subtract the offset.
        mov eax, ecx;                               // Store in eax for later.
        mov ecx, [ecx];                             // Derefernce the value
        mov paramOne, ecx;                          // Store the value in memory.

        // Fix up stack
        add esp,4;                                  // Decrement the stack pointer
        movzx edx, paramInfo.ParameterOneOffset;    // Move the parameter one offset to edx
        dec edx;                                    // Decrement by 1
        cmp edx,0;                                  // Compare offset with zero
        je ParamOneNoShift;                         // If first parameter then no shift.

    ParamOneShiftLoop:
        mov ecx, eax;
        sub ecx, 4;
        mov ecx, [ecx]
        mov [eax], ecx;                             // Copy value over
        sub eax, 4;                                 // Go to next 
        dec edx;                                    // decrement edx
        jnz ParamOneShiftLoop;                      // Loop
    ParamOneNoShift:
        // Check if any parameters should be stored in edx                          
        movzx ecx, paramInfo.ParameterTwoOffset;     
        cmp ecx,0;
        je NoRegister;  

        movzx ecx, paramInfo.ParameterTwoOffset;    // Move the parameter two offset to ecx
        sub ecx, 2;                                 // Increment the offset by two.  One extra for since we already shifted for ecx
        mov eax, 4;                                 // Put 4 in eax
        mul ecx;                                    // Multiple by 4

        // Copy the value from the stack on to the register.
        mov ecx, esp;                               // Move the stack pointer to ecx
        add ecx, eax;                               // Subtract the offset.
        mov eax, ecx;                               // Store in eax for later.
        mov ecx, [ecx];                             // Derefernce the value
        mov paramTwo, ecx;                          // Store the value in memory.           

        // Fix up stack
        add esp,4;                                  // Decrement the stack pointer
        movzx edx, paramInfo.ParameterTwoOffset;    // Move the parameter two offset to ecx
        dec edx;                                    // Decrement by 1
        cmp edx,0;                                  // Compare offset with zero
        je NoRegister;                              // If first parameter then no shift.
    ParamTwoShiftLoop:
        mov ecx, eax;
        sub ecx, 4;
        mov ecx, [ecx]
        mov [eax], ecx;                             // Copy value over
        sub eax, 4;                                 // Go to next 
        dec edx;                                    // decrement edx
        jnz ParamTwoShiftLoop;                      // Loop


    NoRegister:
        mov ecx, paramOne;                          // Copy value from memory to ecx register
        mov edx, paramTwo;                          // 
        push retAddress;
        jmp functionAddress;
    }
}
于 2010-06-26T01:12:51.203 に答える