delphi programming forums mysql charset mget recursive synonimos
free ventrilo servers hosting cs javascript delay python find in list
Back Forum New
abstract:

BUT, IMO, it is safer to store it as a string and do the conversion at the last minute.
Code:

protected
     dllName : String;
Code:

  DllHandle := LoadLibrary(pChar(dllName));


PChar is a pointer!
You can not assign a string to it it simply points to a memory location. It is up to you to allocate memory and stuff a string into it first.
However if you are using a constant string value, Delphi will do the dirty work for you in the following.
Code:
  1. dllName := pChar('OCB.dll');
Copy Code
BUT, IMO, it is safer to store it as a string and do the conversion at the last minute.
Code:
  1. protected
  2.      dllName : String;
Copy Code
Code:
  1.   DllHandle := LoadLibrary(pChar(dllName));
Copy Code

TOP


Originally Posted by clivew
PChar is a pointer!
You can not assign a string to it it simply points to a memory location. It is up to you to allocate memory and stuff a string into it first.
However if you are using a constant string value, Delphi will do the dirty work for you in the following.
Code:
  1. dllName := pChar('OCB.dll');
Copy Code
BUT, IMO, it is safer to store it as a string and do the conversion at the last minute.
Code:
  1. protected
  2.      dllName : String;
Copy Code
Code:
  1.   DllHandle := LoadLibrary(pChar(dllName));
Copy Code
Thanks ... it helped

TOP

hi,
I am having problem in assigning a value to pchar type variable. I have a base class, which is being inherited by child class. The variable is defined in base class in "protected" identifier. child class is assigning the value in its constructor.
Base Class
DELPHI Code:
[url=#top]
[/url]Original
                - DELPHI Code
unit DllBaseClass;

interface
    uses
        Windows;

    type
        TDllBaseClass = class
        protected
            DllHandle : THandle;
            initCalled : boolean;
            dllName : PChar;
        public
            procedure init (); virtual; abstract;
    end;

implementation

end.
  1. unit
  2. DllBaseClass;
  3. interface
  4. uses
  5.         Windows;
  6. type
  7.         TDllBaseClass =
  8. class
  9.         protected
  10.             DllHandle : THandle;
  11.             initCalled :
  12. boolean
  13. ;
  14.             dllName :
  15. PChar
  16. ;
  17.         public
  18. procedure
  19. init
  20. (
  21. )
  22. ; virtual; abstract;
  23. end
  24. ;
  25. implementation
  26. end
  27. .
Copy Code
and the Child Class
DELPHI Code:
[url=#top]
[/url]Original
                - DELPHI Code
unit CodeBase;

interface
    uses
        Windows, DllBaseClass, OtengoGlobal;

    type
        TCodeBase = class (TDllBaseClass)
        private
            DllSetDirectoryPath: procedure(DirectoryPath: PChar); stdcall;
        public
            procedure init (); override;
            Constructor Create(); overload;

            procedure SetDirectoryPath (DirectoryPath : PChar);
    end;

implementation

{ TCodeBase }

constructor TCodeBase.Create;
begin
    inherited;
    dllName := 'OCB.dll'; {Error location}
    init();
end;

procedure TCodeBase.init;
begin
    DllHandle := LoadLibrary(dllName);
    initCalled := true;
end;

procedure TCodeBase.SetDirectoryPath(DirectoryPath: PChar);
begin
    if initCalled = false then
    begin
        init();
    end;

    if @DllSetDirectoryPath = nil then
    begin
        @DllSetDirectoryPath := GetProcAddress(DllHandle, _SetDirectoryPath);
    end;

    DllSetDirectoryPath(DirectoryPath);
end;

end.
  1. unit
  2. CodeBase;
  3. interface
  4. uses
  5.         Windows, DllBaseClass, OtengoGlobal;
  6. type
  7.         TCodeBase =
  8. class
  9. (
  10. TDllBaseClass
  11. )
  12.         private
  13.             DllSetDirectoryPath:
  14. procedure
  15. (
  16. DirectoryPath:
  17. PChar
  18. )
  19. ; stdcall;
  20.         public
  21. procedure
  22. init
  23. (
  24. )
  25. ; override;
  26. Constructor
  27. Create
  28. (
  29. )
  30. ; overload;
  31. procedure
  32. SetDirectoryPath
  33. (
  34. DirectoryPath :
  35. PChar
  36. )
  37. ;
  38. end
  39. ;
  40. implementation
  41. { TCodeBase }
  42. constructor
  43. TCodeBase.
  44. Create
  45. ;
  46. begin
  47. inherited
  48. ;
  49.     dllName :=
  50. 'OCB.dll'
  51. ;
  52. {Error location}
  53.     init
  54. (
  55. )
  56. ;
  57. end
  58. ;
  59. procedure
  60. TCodeBase.
  61. init
  62. ;
  63. begin
  64.     DllHandle := LoadLibrary
  65. (
  66. dllName
  67. )
  68. ;
  69.     initCalled :=
  70. true
  71. ;
  72. end
  73. ;
  74. procedure
  75. TCodeBase.
  76. SetDirectoryPath
  77. (
  78. DirectoryPath:
  79. PChar
  80. )
  81. ;
  82. begin
  83. if
  84. initCalled =
  85. false
  86. then
  87. begin
  88.         init
  89. (
  90. )
  91. ;
  92. end
  93. ;
  94. if
  95. @DllSetDirectoryPath =
  96. nil
  97. then
  98. begin
  99.         @DllSetDirectoryPath := GetProcAddress
  100. (
  101. DllHandle, _SetDirectoryPath
  102. )
  103. ;
  104. end
  105. ;
  106.     DllSetDirectoryPath
  107. (
  108. DirectoryPath
  109. )
  110. ;
  111. end
  112. ;
  113. end
  114. .
Copy Code
Its giving some access violation errors ... .
Error is on line # 25 in child class.



BUT, IMO, it is safer to store it as a string and do the conversion at the last minute.
Code:

protected
     dllName : String;
Code:

  DllHandle := LoadLibrary(pChar(dllName));

TOP

Back Forum