 |
MESSAGE BOARD
|
| View previous topic :: View next topic |
| Author |
Message |
sekar1 Member
Joined: 21 Dec 2006 Posts: 13
|
Posted: 11/05/07 14:28 Post subject: EInvalid Pointer occures when I call DLL function |
|
|
Hi
I have write an DLL function shown below
| Code: |
library N2w;
uses
ShareMem,
SysUtils,
Classes;
function NafToWin(S: string): string; stdcall
var
i: integer;
OneChar: Char;
CharOrder: integer;
NewOrder: integer;
nw : array [0..255] of integer;
begin
NW[13] := 13;
NW[44] := 161 ; //,
NW[32] := -1;
NW[33] := -1 ; // !
NW[34] := -1 ; // """"
NW[35] := -1 ; // #
NW[36] := -1 ; // $
NW[37] := -1 ; // %
NW[38] := -1 ; // &
NW[39] := -1 ; // ; //
NW[40] := -1 ; // (
NW[41] := -1;
NW[42] := -1 ; // *
NW[43] := -1 ; // +
NW[44] := 161 ; //,
NW[45] := -1 ; // -
NW[46] := -1 ; // .
NW[47] := -1 ; // /
NW[48] := -1 ; // 0
NW[49] := -1 ; // 1
NW[50] := -1 ; // 2
NW[51] := -1 ; // 3
NW[52] := -1 ; // 4
NW[53] := -1 ; // 5
NW[54] := -1 ; // 6
NW[55] := -1 ; // 7
NW[56] := -1 ; // 8
NW[57] := -1 ; // 9
NW[58] := -1 ; // :
NW[59] := 186 ; // ; ؛
NW[60] := -1 ; // <
NW[61] := -1 ; // =
NW[62] := -1 ; // >
NW[63] := 191 ; // Print ? > ؟
NW[64] := -1 ; // @
NW[65] := -1 ; // a
NW[66] := -1 ; // b
NW[67] := -1 ; // c
NW[68] := -1 ; // d
NW[69] := -1 ; // e
NW[70] := -1 ; // f
NW[71] := -1 ; // g
NW[72] := -1 ; // h
NW[73] := -1 ; // i
NW[74] := -1 ; // j
NW[75] := -1 ; // k
NW[76] := -1 ; // l
NW[77] := -1 ; // m
NW[78] := -1 ; // n
NW[79] := -1 ; // o
NW[80] := -1 ; // p
NW[81] := -1 ; // q
NW[82] := -1 ; // r
NW[83] := -1 ; // s
NW[84] := -1 ; // t
NW[85] := -1 ; // u
NW[86] := -1 ; // v
NW[87] := -1 ; // w
NW[88] := -1 ; // x
NW[89] := -1 ; // y
NW[90] := -1 ; // z
NW[91] := -1 ; // [
NW[92] := -1 ; // \
NW[93] := -1 ; // ]
NW[94] := -1 ; // ^
NW[95] := -1 ; // _
NW[96] := -1 ; // `
NW[97] := -1 ; // a
NW[98] := -1 ; // b
NW[99] := -1 ; // c
NW[100] := -1 ; // d
NW[101] := -1 ; // e
NW[102] := -1 ; // f
NW[103] := -1 ; //g
NW[104] := -1 ; //h
NW[105] := -1 ; //i
NW[106] := -1 ; //j
NW[107] := -1 ; //k
NW[108] := -1 ; //l
NW[109] := -1 ; //m
NW[110] := -1 ; //n
NW[111] := -1 ; //o
NW[112] := -1 ; //p
NW[113] := -1 ; //q
NW[114] := -1 ; //r
NW[115] := -1 ; //s
NW[116] := -1 ; //t
NW[117] := -1 ; //u
NW[118] := -1 ; //v
NW[119] := -1 ; //w
NW[120] := -1 ; //x
NW[121] := -1 ; //y
NW[122] := -1 ; //z
NW[123] := -1 ; //{
NW[124] := -1 ; // |
NW[125] := -1 ; // }
NW[126] := -1 ; // ~
NW[127] := -1 ; //
NW[128] := -1 ; //
NW[129] := -1 ; //پ
NW[130] := -1 ; //
NW[131] := -1 ; //
NW[132] := -1 ; //
NW[133] := -1 ; //
NW[134] := -1 ; //
NW[135] := -1 ; //
NW[136] := -1 ; //
NW[137] := -1 ; //
NW[138] := -1 ; //
NW[139] := -1 ; //
NW[140] := -1 ; //
NW[141] := -1 ; // چ
NW[142] := -1 ; // ژ
NW[143] := -1 ; //
NW[144] := -1 ; // گ
NW[145] := -1 ; //
NW[146] := -1 ; //
NW[147] := -1 ; //
NW[148] := -1 ; //
NW[149] := -1 ; //
NW[150] := -1 ; //
NW[151] := -1 ; //
NW[152] := -1 ; //
NW[153] := -1 ; //
NW[154] := -1 ; //
NW[155] := -1 ; //
NW[156] := -1 ; //
NW[157] := -1 ; //
NW[158] := -1 ; //
NW[159] := -1 ; //
NW[160] := -1 ; //
NW[161] := -1 ; // ،
NW[162] := -1 ; //
NW[163] := -1 ; //
NW[164] := -1 ; //
NW[165] := 193 ; // hamza
NW[166] := 194 ; //
NW[167] := 195 ; //
NW[168] := 196 ; //
NW[169] := 197 ; //
NW[170] := 198 ; //
NW[171] := 199 ; //
NW[172] := 200 ; //
NW[173] := 201 ; //
NW[174] := -1 ; //
NW[175] := -1 ; //
NW[176] := 124 ; //
NW[177] := 124 ; //
NW[178] := 124 ; //
NW[179] := 124 ; //
NW[180] := 124 ; //
NW[181] := 124 ; //
NW[182] := 124 ; //
NW[183] := 124 ; //
NW[184] := 124 ; //
NW[185] := 124 ; //
NW[186] := 124 ; // ؛
NW[187] := 124 ; //
NW[188] := 124 ; //
NW[189] := 124 ; //
NW[190] := 124 ; //
NW[191] := 124 ; // ؟
NW[192] := 124 ; //
NW[193] := 124 ; // ء
NW[194] := 124 ; // آ
NW[195] := 124 ; // أ
NW[196] := 45 ; // ؤ -
NW[197] := 124 ; // إ
NW[198] := 124 ; // ئ
NW[199] := 124 ; // ا
NW[200] := 124 ; // ب
NW[201] := 124 ; // ة
NW[202] := 124 ; // ت
NW[203] := 124 ; // ث
NW[204] := 124 ; // ج
NW[205] := 124 ; // ح
NW[206] := 124 ; // خ
NW[207] := 124 ; // د
NW[208] := 124 ; // ذ
NW[209] := 124 ; // ر
NW[210] := 124 ; // ز
NW[211] := 124 ; // س
NW[212] := 124 ; // ش
NW[213] := 124 ; // ص
NW[214] := 124 ; // ض
NW[215] := 124 ; //
NW[216] := 124 ; // ط
NW[217] := 124 ; // ظ
NW[218] := 124 ; // ع
NW[219] := 124 ; // غ
NW[220] := 124 ; // ـ
NW[221] := 124 ; // ف
NW[222] := 124 ; // ق
NW[223] := 124 ; // ك
NW[224] := 202 ; //
NW[225] := 203 ; // ل
NW[226] := 204 ; //
NW[227] := 205 ; // م
NW[228] := 206 ; // ن
NW[229] := 207 ; // ه
NW[230] := 208 ; // و
NW[231] := 209 ; //
NW[232] := 210 ; //
NW[233] := 211 ; //
NW[234] := 212 ; //
NW[235] := 213 ; //
NW[236] := 214 ; // ى
NW[237] := 216 ; // ي
NW[238] := 217 ; //
NW[239] := 218 ; //
NW[240] := 219 ; // ً
NW[241] := 220 ; // ٌ
NW[242] := 221 ; // ٍ
NW[243] := 222 ; // َ
NW[244] := 223 ; //
NW[245] := 225 ; // ُ
NW[246] := 227 ; // ِ
NW[247] := -1 ; // ; //?
NW[248] := 228 ; // ّ
NW[249] := 229 ; //
NW[250] := -1 ; // ْ ; //?
NW[251] := 230 ; //
NW[252] := 236 ; //
NW[253] := 237 ; //
NW[254] := -1 ; // ?
NW[255] := -1 ; //
for i := 1 to length(S) do
begin
OneChar := S[i];
CharOrder := Ord(OneChar);
NewOrder := NW[CharOrder];
if NewOrder > 0 then
begin
S[i] := Chr(NewOrder);
end;
end;
result := S;
end;
{$R *.res}
exports
NafToWin;
begin
end.
|
===================================
I call that function in this unit as showen below ; the function its works ok and gives a required result but when I close the form there is an error occures the error msg is "raised exception class EinvalidPointer with message Invalid pointer operation" process Stop.
someone help me to aviod this . and aslo want to know wt may be the other reasons for getting this type of error and how t osolve this?
| Code: |
// the call unit
unit Unit1;
interface
uses
ShareMem,Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons;
type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
Edit1: TEdit;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function NafToWin(S: string): string;stdcall; external 'N2w.dll';
type
TfuncN2w = function(s:string):string;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
x,y : string;
hdll : THandle;
funcN2w : TfuncN2w;
Dwreason : dword;
begin
x := edit1.text;
//===========
hdll := loadlibrary(pchar('N2w'));
if hdll <> 0 then
funcN2w := getprocaddress(hdll,pchar('NafToWin'));
if @funcN2w <> nil then
showmessage(NafToWin(x));
freelibrary(hdll);
//========
end;
end.
|
//Mod edit: USE THE [ c o d e ]!
|
|
| Back to top |
|
 |
Johnny_Bit Administrator
Joined: 15 Jun 2003 Posts: 397
|
Posted: 11/05/07 18:39 Post subject: |
|
|
you had mixed up two different ways of handing libraries, so probably you broke something in very uncommon way.
First of all when using static lib loading like this:
| Code: |
function NafToWin(S: string): string;stdcall; external 'N2w.dll';
|
It's guaranteed that this function exists, because your app would die painfully other way around. So knowing that why do you dynamically load lib, then call statically linked function and then free all library pointers? I think that on normal OS, memory handling would go in a way that avoids dynamic linking the same lib over and over again. Probably free library frees all pointers, even static one, so when app tires to destroy it's static pointer it goes bananas and EInvalidPointer on demand.
_________________ Thou shalt write the code, not connect the bricks. |
|
| Back to top |
|
 |
sekar1 Member
Joined: 21 Dec 2006 Posts: 13
|
Posted: 14/05/07 12:30 Post subject: |
|
|
thanks FYI,
one more doubt i created one dll with five to six functions and i given the static linking like you explained above...
do it create any problem like this same (invalid pointer operation) when you are calling more than two or three functions from Dll in a functuion of main form.?
since i am facing the same problem after i did the above procedure exp by jhony?
|
|
| Back to top |
|
 |
Johnny_Bit Administrator
Joined: 15 Jun 2003 Posts: 397
|
Posted: 14/05/07 17:45 Post subject: |
|
|
I've never had any problems with calling statically linked funcions/procedures. Well, to be honest, you do too, whenever your ap aclls Windows API it simply calls linked dll.
_________________ Thou shalt write the code, not connect the bricks. |
|
| Back to top |
|
 |
Kambiz Administrator

Joined: 07 Mar 2003 Posts: 1231 Location: Tehran, Iran
|
Posted: 14/05/07 21:06 Post subject: |
|
|
Maybe it's because of using String data type.
_________________ Kambiz
 |
|
| Back to top |
|
 |
sekar1 Member
Joined: 21 Dec 2006 Posts: 13
|
Posted: 16/05/07 06:42 Post subject: |
|
|
okey,
do u clarify my questions?
1. Which is the problem, declaration of string datatype for Dll functions or in the delphi functions?
in my case all the dll function i created will return integer type and i/p to the function is also the integer type;
using the dll function, i ad written code for populating delphi class object, i used the constructor properly for creating the object and made all the declaration correctly.
the thing is the functions are working fine and the object is populating with the readed data (From external file) by using the dll functions. but before the execution of the last END statement of the delphi main function, it is showing this error of INVALID POINTER OPERATION.
wt is the cause of the error?
(due to some problems, i am not able to send the exact code also. forgive for that and give a solution, i am trying to diagonsis this error for more than a week)
|
|
| Back to top |
|
 |
Kambiz Administrator

Joined: 07 Mar 2003 Posts: 1231 Location: Tehran, Iran
|
Posted: 16/05/07 11:43 Post subject: |
|
|
I suggest to remove ShareMem unit from the uses clause and use C-style string manipulation.
| Code: | library N2W;
const
NW: array[Char] of Char = (
{ #000 } #000,
{ #001 } #000,
{ #002 } #000,
{ #003 } #000,
{ #004 } #000,
{ #005 } #000,
{ #006 } #000,
{ #007 } #000,
{ #008 } #000,
{ #009 } #000,
{ #010 } #000,
{ #011 } #000,
{ #012 } #000,
{ #013 } #013,
{ #014 } #000,
{ #015 } #000,
{ #016 } #000,
{ #017 } #000,
{ #018 } #000,
{ #019 } #000,
{ #020 } #000,
{ #021 } #000,
{ #022 } #000,
{ #023 } #000,
{ #024 } #000,
{ #025 } #000,
{ #026 } #000,
{ #027 } #000,
{ #028 } #000,
{ #029 } #000,
{ #030 } #000,
{ #031 } #000,
{ #032 } #000,
{ #033 } #000,
{ #034 } #000,
{ #035 } #000,
{ #036 } #000,
{ #037 } #000,
{ #038 } #000,
{ #039 } #000,
{ #040 } #000,
{ #041 } #000,
{ #042 } #000,
{ #043 } #000,
{ #044 } #161,
{ #045 } #000,
{ #046 } #000,
{ #047 } #000,
{ #048 } #000,
{ #049 } #000,
{ #050 } #000,
{ #051 } #000,
{ #052 } #000,
{ #053 } #000,
{ #054 } #000,
{ #055 } #000,
{ #056 } #000,
{ #057 } #000,
{ #058 } #000,
{ #059 } #186,
{ #060 } #000,
{ #061 } #000,
{ #062 } #000,
{ #063 } #191,
{ #064 } #000,
{ #065 } #000,
{ #066 } #000,
{ #067 } #000,
{ #068 } #000,
{ #069 } #000,
{ #070 } #000,
{ #071 } #000,
{ #072 } #000,
{ #073 } #000,
{ #074 } #000,
{ #075 } #000,
{ #076 } #000,
{ #077 } #000,
{ #078 } #000,
{ #079 } #000,
{ #080 } #000,
{ #081 } #000,
{ #082 } #000,
{ #083 } #000,
{ #084 } #000,
{ #085 } #000,
{ #086 } #000,
{ #087 } #000,
{ #088 } #000,
{ #089 } #000,
{ #090 } #000,
{ #091 } #000,
{ #092 } #000,
{ #093 } #000,
{ #094 } #000,
{ #095 } #000,
{ #096 } #000,
{ #097 } #000,
{ #098 } #000,
{ #099 } #000,
{ #100 } #000,
{ #101 } #000,
{ #102 } #000,
{ #103 } #000,
{ #104 } #000,
{ #105 } #000,
{ #106 } #000,
{ #107 } #000,
{ #108 } #000,
{ #109 } #000,
{ #110 } #000,
{ #111 } #000,
{ #112 } #000,
{ #113 } #000,
{ #114 } #000,
{ #115 } #000,
{ #116 } #000,
{ #117 } #000,
{ #118 } #000,
{ #119 } #000,
{ #120 } #000,
{ #121 } #000,
{ #122 } #000,
{ #123 } #000,
{ #124 } #000,
{ #125 } #000,
{ #126 } #000,
{ #127 } #000,
{ #128 } #000,
{ #129 } #000,
{ #130 } #000,
{ #131 } #000,
{ #132 } #000,
{ #133 } #000,
{ #134 } #000,
{ #135 } #000,
{ #136 } #000,
{ #137 } #000,
{ #138 } #000,
{ #139 } #000,
{ #140 } #000,
{ #141 } #000,
{ #142 } #000,
{ #143 } #000,
{ #144 } #000,
{ #145 } #000,
{ #146 } #000,
{ #147 } #000,
{ #148 } #000,
{ #149 } #000,
{ #150 } #000,
{ #151 } #000,
{ #152 } #000,
{ #153 } #000,
{ #154 } #000,
{ #155 } #000,
{ #156 } #000,
{ #157 } #000,
{ #158 } #000,
{ #159 } #000,
{ #160 } #000,
{ #161 } #000,
{ #162 } #000,
{ #163 } #000,
{ #164 } #000,
{ #165 } #193,
{ #166 } #194,
{ #167 } #195,
{ #168 } #196,
{ #169 } #197,
{ #170 } #198,
{ #171 } #199,
{ #172 } #200,
{ #173 } #201,
{ #174 } #000,
{ #175 } #000,
{ #176 } #124,
{ #177 } #124,
{ #178 } #124,
{ #179 } #124,
{ #180 } #124,
{ #181 } #124,
{ #182 } #124,
{ #183 } #124,
{ #184 } #124,
{ #185 } #124,
{ #186 } #124,
{ #187 } #124,
{ #188 } #124,
{ #189 } #124,
{ #190 } #124,
{ #191 } #124,
{ #192 } #124,
{ #193 } #124,
{ #194 } #124,
{ #195 } #124,
{ #196 } #045,
{ #197 } #124,
{ #198 } #124,
{ #199 } #124,
{ #200 } #124,
{ #201 } #124,
{ #202 } #124,
{ #203 } #124,
{ #204 } #124,
{ #205 } #124,
{ #206 } #124,
{ #207 } #124,
{ #208 } #124,
{ #209 } #124,
{ #210 } #124,
{ #211 } #124,
{ #212 } #124,
{ #213 } #124,
{ #214 } #124,
{ #215 } #124,
{ #216 } #124,
{ #217 } #124,
{ #218 } #124,
{ #219 } #124,
{ #220 } #124,
{ #221 } #124,
{ #222 } #124,
{ #223 } #124,
{ #224 } #202,
{ #225 } #203,
{ #226 } #204,
{ #227 } #205,
{ #228 } #206,
{ #229 } #207,
{ #230 } #208,
{ #231 } #209,
{ #232 } #210,
{ #233 } #211,
{ #234 } #212,
{ #235 } #213,
{ #236 } #214,
{ #237 } #216,
{ #238 } #217,
{ #239 } #218,
{ #240 } #219,
{ #241 } #220,
{ #242 } #221,
{ #243 } #222,
{ #244 } #223,
{ #245 } #225,
{ #246 } #227,
{ #247 } #000,
{ #248 } #228,
{ #249 } #229,
{ #250 } #000,
{ #251 } #230,
{ #252 } #236,
{ #253 } #237,
{ #254 } #000,
{ #255 } #000
);
procedure NafToWin(S: PChar); stdcall;
begin
while S^ <> #0 do
begin
if NW[S^] <> $0 then
S^ := NW[S^];
Inc(S);
end;
end;
exports
NafToWin;
begin
end. |
| Code: | procedure NafToWin(S: PChar); stdcall; external 'N2W.dll';
procedure TForm1.BitBtn1Click(Sender: TObject);
var
S: String;
begin
S := Edit1.Text;
UniqueString(S); // This call is important
NafToWin(PChar(S));
ShowMessage(S);
end; |
This code is also much faster than the old one.
_________________ Kambiz
 |
|
| Back to top |
|
 |
|
Add to favorites
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|