Page 1 of 1

EInvalid Pointer occures when I call DLL function

PostPosted: May 11th, 2007, 2:28 pm
by sekar1
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 ; //

PostPosted: May 11th, 2007, 6:39 pm
by Johnny_Bit
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: Select all
 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.

PostPosted: May 14th, 2007, 12:30 pm
by sekar1
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?

PostPosted: May 14th, 2007, 5:45 pm
by Johnny_Bit
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.

PostPosted: May 14th, 2007, 9:06 pm
by Kambiz
Maybe it's because of using String data type.

PostPosted: May 16th, 2007, 6:42 am
by sekar1
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)

PostPosted: May 16th, 2007, 11:43 am
by Kambiz
I suggest to remove ShareMem unit from the uses clause and use C-style string manipulation.

Code: Select all
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: Select all
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.