Recent

Author Topic: [SOLVED] Initialize / use pointer  (Read 920 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[SOLVED] Initialize / use pointer
« on: November 05, 2021, 08:38:29 am »
I have this type and a var

Code: Pascal  [Select][+][-]
  1. type
  2.   Pndu_WLAN_CONNECTION_PARAMETERS = ^Tndu_WLAN_CONNECTION_PARAMETERS;
  3.  
  4.   Tndu_WLAN_CONNECTION_PARAMETERS = record
  5.     wlanConnectionMode: Tndu_WLAN_CONNECTION_MODE;
  6.     strProfile: LPCWSTR;
  7.     pDot11Ssid: Pndu_DOT11_SSID;
  8.     pDesiredBssidList: Pndu_DOT11_BSSID_LIST;
  9.     dot11BssType: Tndu_DOT11_BSS_TYPE;
  10.     dwFlags: DWORD;
  11.   end;
  12.  
  13. var
  14.   pConnectionParameters : Pndu_WLAN_CONNECTION_PARAMETERS;
  15.  

Every time I try to assign values (for example)

Code: Pascal  [Select][+][-]
  1.   pConnectionParameters^.dwFlags := 0;
  2.  

I get a SIGSEGV.

What am I missing?
« Last Edit: November 05, 2021, 09:24:37 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: Initialize / use pointer
« Reply #1 on: November 05, 2021, 08:45:51 am »
I have this type and a var

Code: Pascal  [Select][+][-]
  1. type
  2.   Pndu_WLAN_CONNECTION_PARAMETERS = ^Tndu_WLAN_CONNECTION_PARAMETERS;
  3.  
  4.   Tndu_WLAN_CONNECTION_PARAMETERS = record
  5.     wlanConnectionMode: Tndu_WLAN_CONNECTION_MODE;
  6.     strProfile: LPCWSTR;
  7.     pDot11Ssid: Pndu_DOT11_SSID;
  8.     pDesiredBssidList: Pndu_DOT11_BSSID_LIST;
  9.     dot11BssType: Tndu_DOT11_BSS_TYPE;
  10.     dwFlags: DWORD;
  11.   end;
  12.  
  13. var
  14.   pConnectionParameters : Pndu_WLAN_CONNECTION_PARAMETERS;
  15.  

Every time I try to assign values

Code: Pascal  [Select][+][-]
  1.   pConnectionParameters^.dwFlags := 0;
  2.  

I get a SIGSEGV.

What am I missing?
You have a pointer to a record _definition_ but you don't have a record to point to.

have something like this:
Code: Pascal  [Select][+][-]
  1. var
  2.  TnduVar : Tndu_WLAN...etc_record;
  3.  PconnectionParameters : ... {as you have it now }
  4.  
  5. ...
  6.  {somewhere in the code}
  7.  
  8.   PConnectionParameters := @TndvVar;
  9.  
  10. { NOW you can set the dwFlags to something }
  11.  
  12.   PConnectionParameters^.dwFlags := {whatever you want }
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14168
  • Probably until I exterminate Putin.
Re: Initialize / use pointer
« Reply #2 on: November 05, 2021, 08:49:46 am »
Looks like the record is not allocated. Call new() (and later dispose())
Something like:
Code: Pascal  [Select][+][-]
  1. var
  2.   pConnectionParameters : Pndu_WLAN_CONNECTION_PARAMETERS;
  3. begin
  4.  // just to show the logic.
  5.  New(pConnectionParameters);
  6.  pConnectionParameters^.dwFlags := 0;
  7.  // and somewhere later.
  8.  Dispose(pConnectionParameters);
  9. end.
Alternatively you can declare the var as the T version instead of the P version. That also gives you an accessible record and does not need new/dispose.

If the record already exists use the code by 440bx (our posts crossed)

« Last Edit: November 05, 2021, 08:57:09 am by Thaddy »
Specialize a type, not a var.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Initialize / use pointer
« Reply #3 on: November 05, 2021, 09:23:50 am »
Thanks all
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018