Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 135,922 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,595 people online right now. Registration is fast and FREE... Join Now!




Updating a .net listview from unmanaged code

 
Reply to this topicStart new topic

Updating a .net listview from unmanaged code, Problems while updating a managed listview from unmanaged code...

achintya
20 May, 2008 - 03:35 PM
Post #1

New D.I.C Head
*

Joined: 20 May, 2008
Posts: 2

Hi all,
I have been working on a project which was till now in unmanaged C and C++.
We have been trying to port the front end only to c# while retaining the backend in unmanaged c and c++.
To do that, we have converted the backend into a dll and call it using P/Invoke.
The code updates a listview dynamically but the earlier listview was in unmanaged.
Now here's the weird part. The function which takes the listview handle and updates it uses sendmessage to do that as below:

CODE

void UpdateDrives(HWND hListView)
{
LVITEM LvItem;
//some calculations here
LvItem.iItem = line++; //line is the number of next item
LvItem.pszText = drive;
LvItem.mask = LVIF_TEXT;
SendMessage (hComboBox,LVM_INSERTITEM,0,(LPARAM)&LvItem);
}


I am calling this from managed code as this:
CODE

void GetDrives()
{
//Wrapper class is where the P/Invoke decl is
WrapperClass.UpdateDrives(driveListView.Handle);
}



After the execution is done, I can see that the listview is updated (all the fields are correctly set) but the count of the items is still 0. When I try and click on the listview, the application throws an ArgumentOutOfRangeException saying that the index is invalid.

This has been driving me mad. Can any one please tell me what am I doing wrong here?

Thanks.

This post has been edited by achintya: 20 May, 2008 - 03:38 PM
User is offlineProfile CardPM
+Quote Post

zakary
RE: Updating A .net Listview From Unmanaged Code
21 May, 2008 - 03:39 AM
Post #2

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 404



Thanked: 6 times
Dream Kudos: 175
My Contributions
can you post your unmanaged code that raps your dll. That is the code that matters your c and c++ methods you just need to know the parameters and return type.


User is online!Profile CardPM
+Quote Post

achintya
RE: Updating A .net Listview From Unmanaged Code
21 May, 2008 - 04:21 AM
Post #3

New D.I.C Head
*

Joined: 20 May, 2008
Posts: 2

QUOTE(zakary @ 21 May, 2008 - 04:39 AM) *

can you post your unmanaged code that raps your dll. That is the code that matters your c and c++ methods you just need to know the parameters and return type.


The parameters and return type have no role here. What I did was to create a test project to test this problem and have posted the code in the previous post. Anyway, I could illustrate the problem a bit more here I guess.

CODE

//Wrapper class for the Pinvoke
class WrapperClass
    {
[DllImport("unmanaged.dll")
        public static extern int PopulateListView(IntPtr hwndDlg);
}


Now I tried something really simple. I created an unmanaged project and just wrote a simple function to update the list view.

CODE

__declspec(dllexport) void PopulateListView(HWND hTree)
{
LVITEM LvItem;
//attempt to insert 10 listview items
for(int i=0;i<10;i++)
{
LvItem.iIndex = i;
LvItem.pszText = "Hello"; //some string value
LvItem.mask = LVIF_TEXT;
SendMessage (hTree,LVM_INSERTITEM,0,(LPARAM)&LvItem);
//now insert some attributes for each item
for(int j = 0;j<4;j++)
{
LvItem.iSubItem = j;
LvItem.mask = LVIF_TEXT;
SendMessage (hTree,LVM_SETITEM,0,(LPARAM)&LvItem);
}
}
}


I am calling this ofcourse as mentioned earlier, ie passing the handle of the listview control.

This sets my listview properly and I can see all the items in the UI. But as soon as I click on it, or do a slight drag, it throws an ArgumentOutOfRangeException.
Upon debugging, I found that after the unmanaged code has finished executing, the count of items in the listview still remains 0! blink.gif How is this possible? I can see the bloody items in the UI, so why aren't they showing up in the count!

This has driven me nuts and now I am contemplating passing delegates to just get the values from the unmanaged code and update the list view myself in the managed code. But this would involve a major rewrite of the functions.

I suppose that the underlying architecture of all the controls should be the same, and any message sent to an unmanaged or managed code should do just fine. However, I may be wrong, and it certainly looks like the case mad.gif
Help!!

User is offlineProfile CardPM
+Quote Post

zakary
RE: Updating A .net Listview From Unmanaged Code
21 May, 2008 - 05:13 AM
Post #4

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 404



Thanked: 6 times
Dream Kudos: 175
My Contributions
what is LVITEM is this a struct when you wrap [DllImport("unmanaged.dll") do you have handle returned meaning you use the ref key work to return a handle to your dll

Example:
csharp

const string dll_name = "unmanaged.dll";

[DllImport(dll_name)]
public static extern int CPPReference(ref IntPtr handle);


then your managed code looks like this

csharp

IntPtr ptrTo_Handle;
CPPReference(ref ptrTo_Handle);


now you will have the handle to you c++ code and your code uptop
csharp

// your code
WrapperClass.UpdateDrives(driveListView.Handle);
// should look like
WrapperClass.UpdateDrives(ptrTo_Handle);



User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:12AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month