这里贴出关键部分的代码:<br><br>type<br> TArg = record<br> ArgType: Integer;<br> S: String;<br> I: Integer;<br> D: Double;<br> end;<br><br> TWords = array of String;<br><br> TFunInfo = record<br> DllName: String;<br> FunName: String;<br> Params: array of TArg;<br> Ret: Integer;<br> end;<br><br><br><br>..................<br>function ParseArg(S: String): TArg;<br>var<br> m: Integer;<br> t1, t2: String;<br> c: Char;<br>begin<br> Result.ArgType := 0;<br> Result.S := '';<br> Result.I := 0;<br> Result.D := 0.0;<br> m := Pos(':', S);<br> if m > 0 then<br> begin<br> t1 := UpperCase(Copy(S, 1, m - 1));<br> t2 := Copy(S, m + 1, Length(S));<br> end;<br> if Length(t1) = 1 then<br> begin<br> c := t1[1];<br> case c of<br> 'S': //String<br> begin<br> Result.ArgType := 1;<br> Result.S := UnQuoteString(Trim(t2));<br> end;<br> 'I': //Integer<br> begin<br> Result.ArgType := 2;<br> Result.I := StrToIntDef(t2, 0);<br> end;<br> 'D', 'F': //Double<br> begin<br> Result.ArgType := 3;<br> Result.D := StrToFloatDef(t2, 0.0);<br> end;<br> end;<br> end<br> else<br> begin<br> if (t1 = 'INT') or (t1 = 'INTEGER') then<br> begin<br> Result.ArgType := 1;<br> Result.S := UnQuoteString(Trim(t2));<br> end<br> else if (t1 = 'STR') or (t1 = 'STRING') then<br> begin<br> Result.ArgType := 2;<br> Result.I := StrToIntDef(t2, 0);<br> end<br> else if (t1 = 'FLOAT') or (t1 = 'DOUBLE') then<br> begin<br> Result.ArgType := 3;<br> Result.D := StrToFloatDef(t2, 0.0);<br> end<br> else if (t1 = 'VI') or (t1 = 'VINTEGER') then<br> begin<br> Result.ArgType := 12;<br> Result.I := StrToIntDef(t2, 0);<br> end;<br> end;<br>end;<br><br>function ParseFun(S: String): TFunInfo;<br>var<br> m: Integer;<br> v: TWords;<br> i: Integer;<br>begin<br> Result.DllName := '';<br> Result.FunName := '';<br> Result.Ret := 0;<br> v := SplitWithSpace(S, '"');<br> if Length(v) > 0 then<br> begin<br> m := Pos('::', v[0]);<br> if m > 0 then<br> begin<br> Result.DllName := Copy(v[0], 1, m - 1);<br> Result.DllName := UnQuoteString(Result.DllName);<br> Result.FunName := Copy(v[0], m + 2, Length(v[0]));<br> end;<br> end;<br> if Result.DllName <> '' then<br> begin<br> SetLength(Result.Params, Length(v) - 1);<br> for i := 1 to Length(v) - 1 do<br> begin<br> Result.Params[i - 1] := ParseArg(v);<br> end;<br> end;<br>end;<br><br>function RunDllFun(var fun: TFunInfo): Integer;<br>var<br> i, r, t: Integer;<br> d: Double;<br> pd: PIntegerArray;<br> t1, t2: Integer;<br> dll: Integer;<br> f: Pointer;<br> p: PChar;<br>begin<br> Result := 0;<br> dll := LoadLibrary(PChar(fun.DllName));<br> try //finally<br> try //except<br> if dll <> 0 then //load ok<br> begin<br> f := GetProcAddress(dll, PChar(fun.FunName));<br> if Assigned(f) then<br> begin<br> for i := Length(fun.Params) - 1 downto 0 do<br> begin<br> case fun.Params.ArgType of<br> 0:<br> begin<br> asm<br> push 0<br> end;<br> end;<br> 1:<br> begin<br> SetLength(fun.Params.S, 500);<br> p := PChar(fun.Params.S);<br> asm<br> push p<br> end;<br> end;<br> 2:<br> begin<br> t := fun.Params.I;<br> asm<br> push t<br> end;<br> end;<br> 3:<br> begin<br> d := fun.Params.D;<br> pd := @d;<br> t1 := pd[0];<br> t2 := pd[1];<br> asm<br> push t2<br> push t1<br> end;<br> end;<br> 12: //整数变参<br> begin<br> t := Integer(@(fun.Params.I));<br> asm<br> push t<br> end;<br> end;<br> end;<br> end;<br> // call the function<br> asm<br> call f;<br> mov r, eax<br> end;<br> fun.Ret := r;<br> end<br> else<br> begin<br> Result := -3;<br> end;<br> end<br> else<br> begin<br> Result := -4;<br> end;<br> except<br> Result := -2;<br> end;<br> finally<br> if dll <> 0 then FreeLibrary(dll);<br> end;<br>end;<br><br>function RunDLL2(S: String; var Fun: TFunInfo): Integer;<br>begin<br> Result := -1;<br> Fun := ParseFun(S);<br> if Fun.DllName <> '' then<br> begin<br> try<br> Result := RunDllFun(Fun);<br> except<br> ShowMessage('发生了调用异常');<br> end;<br> end<br> else<br> raise Exception.Create('解析失败,可能是格式不正确');<br><br>end;<br><br><br>.........................................<br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br> s: String;<br> i, r: Integer;<br> t: String;<br> FunInfo: TFunInfo;<br> function GetPaStr(pa: TArg): String;<br> begin<br> case pa.ArgType of<br> 0: Result := 'Null:0';<br> 1: Result := 'S:' + PChar(pa.S);<br> 2: Result := 'I:' + IntToStr(pa.I);<br> 3: Result := 'D:' + FloatToStr(pa.D);<br> 12: Result := 'VI:' + IntToStr(pa.I);<br> else Result := 'UnKnown';<br> end;<br> end;<br>begin<br> //表达式调用<br> s := Edit1.Text;<br> if Trim(s) <> '' then<br> begin<br> r := RunDLL2(s, FunInfo);<br> if r = 0 then<br> begin<br> for i := 0 to Length(FunInfo.Params) - 1 do<br> begin<br> if i = 0 then t := GetPaStr(FunInfo.Params)<br> else t := t + #13#10 + GetPaStr(FunInfo.Params);<br> end;<br> ShowMessage('调用成功,返回值:' + IntToStr(FunInfo.Ret) + #13#10 +<br> '参数列表:' + #13#10 + t);<br> end<br> else<br> ShowMessage('调用失败,错误代码:'+ IntToStr(r));<br> end<br> else<br> ShowMessage('请输入函数名称及参数');<br>end;<br>