Solved

Using ListView macros from CommCtrl?

Posted on 2002-06-11
12
479 Views
Last Modified: 2012-06-21
I'm trying to use ListView macros from the CommCtrl unit to add items on the main application from a DLL file. This works fine, but I realized that when I try to read the items added by my DLL from my main app. I got a "Access violation... Read of address 00000004" :-(

Here is the code I use to add lines from my DLL:

//---------------------------------//

procedure AddLine(Line: string);
var
  i: integer;
  ListItem: LV_ITEM;
begin
  i := ListView_GetItemCount(CtrlOutput);
  ListItem.mask := LVIF_TEXT;
  ListItem.iItem := i;
  ListItem.iSubItem := 0;
  ListItem.pszText := PChar(Line);
  ListItem.cchTextMax := 255;
  i := ListView_InsertItem(CtrlOutput, ListItem);
  ListView_EnsureVisible(CtrlOutput, i, false);
end;

//---------------------------------//

CtrlOutput is the handle to my ListView on the main app.

Can someone please tell me what I'm doing wrong.
0
Comment
Question by:Fraction
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7071760
and how do you read the items from the main app?
0
 
LVL 1

Author Comment

by:Fraction
ID: 7071784
I've tried two ways.

Try 1 (this one use to work):
//---------------------------------//
procedure TForm1.ListView1DblClick(Sender: TObject);
var
  s: string;
begin
  if (Sender as TListView).SelCount>0 then begin
    s := (Sender as TListView).Selected.Caption;
    ...
  end;
end;
//---------------------------------//

Try 2 (I'm improvising):
//---------------------------------//
procedure TForm1.ListView1DblClick(Sender: TObject);
var
  c: array[0..255] of char;
  s: string;
begin
  if (Sender as TListView).SelCount>0 then begin
    ListView_GetItemText(ListView1.Handle, (Sender as TListView).Selected.Index, 0, c, 255);
    ...
  end;
end;
//---------------------------------//
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7071802
First of all, check and see if Selected is nil?

with Sender as TListView do
begin
  if not Assigned(Selected) then
    ShowMessage('Nothing selected!')
  else ShowMessage(Selected.Caption);
end;
0
 
LVL 1

Author Comment

by:Fraction
ID: 7071812
There is nothing selected thogh I dblclick right on the items, that's why I thought the items is not properly added.

Strange by the way, SelCount>0 and Selected is NIL...
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7071830
Hmm... strange... Some questions...

1. is your listview virtual?
2. is the total items in the listview = listview.items.count?
3. if you do a loop to find the selected item, does it work?... as in:
  for i := 0 to ListView1.Count - 1 do
    if ListView1.Items[i].Selected then
      ShowMessage('Item ' + IntToStr(i) + ' is selected...'#13'Caption is ' + ListView1.Items[i].Caption);
0
 
LVL 1

Author Comment

by:Fraction
ID: 7072405
1. No, my ListView is not virtual.
2. Yes, both total items and selected items is equal to Items.Count and SelCount.
3. I tried the loop, but all my items are NIL, so I can't even read Items[i].Selected.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:Fraction
ID: 7072841
Guess this question was more difficult than I thoght, so I increase points to 100.
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7074650
okay fraction why don't you publish the source for both your app and your dll and i'll see what i can do from there
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9010646
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ with No Refund

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0
 
LVL 1

Author Comment

by:Fraction
ID: 9015955
sorry, i dono how to "PAQ", but you're welcome to delete this question.
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9017761
Sorry, you cant do that... It will be PAQ'd by the moderation team. If you wish to give someone the points, you may do so.

Paul (pnh73)
EE Cleanup Volunteer
0
 
LVL 6

Accepted Solution

by:
Mindphaser earned 0 total points
ID: 9102331
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now