Solved

LVM_SORTITEMS

Posted on 2007-03-22
11
911 Views
Last Modified: 2013-11-20
Urgent!

Can anybody help me call the LVM_SORTITEMS windows message in .NET?
I'm trying to make a call to the Win32 API directly through using SendMessage. Does anyone know how to do this?

Greatly appreciated.
0
Comment
Question by:maloriopolium
  • 4
  • 3
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18777258
You can do that// =============================================================
// Comparison routine for sorting ListView on a column
// =============================================================
int CALLBACK ListViewCompareProc(LPARAM lParam1, LPARAM lParam2,
LPARAM lParamSort )
{
      int iResult;

   // MessageBox( hwnd, "Doing sort compare", "SortMsg", MB_OK );
   
      switch( lParamSort )
   {
         case 0:                                                // sub-item 0; file name
            iResult = 0;
            break;

      case 1:                                                // sub-item 1; folder size
      case 2:                                                // sub-item 2; number of files
            if ( lParam1 > lParam2 )
               iResult = 1;
         else
         if ( lParam1 < lParam2 )
               iResult = -1;
         else
               iResult = 0;
         break;
   }


   
   return iResult;
}


SendMessage( hwndList, LVM_SORTITEMS,(WPARAM)(LPARAM)(nSomeId),(LPARAM)(PFNLVCOMPARE)ListViewCompareProc );
 like
0
 

Author Comment

by:maloriopolium
ID: 18777349
Hi jkr,

I am trying to do this in C# .NET. Can this be done? I know you can make various other Win32 API calls in C# but just not sure how to go about this one since one of the parameters is a pointer to a function (PFNLVCOMPARE)

Thanks.
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 18777388
Hm, I doubt that you'll be able to pass a function pointer to a C# function. What you could do is creating a "native" Win32 DLL that contains such a sor function and call it via P/Invoke. See http://www.codeproject.com/dotnet/PInvoke.asp ("Using Platform Invoke")
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 19

Assisted Solution

by:alb66
alb66 earned 250 total points
ID: 18778040
If you are using a ListView you can call the Sort() method.
See MSDN documentation at http://msdn2.microsoft.com/en-us/library/system.windows.forms.listview.sort(VS.71).aspx
0
 

Author Comment

by:maloriopolium
ID: 18784720
Thanks jkr,

I just read that article you linked, it sys,
"Callback functions and passing arrays as parameters involve some more complications and can be the subject for the next article."

which sort of implies that it IS possible to pass function pointers to C#?
Do you know or can anyone point me to something that discusses the passing of callback functions or function pointers as a parameter to a Win32 API function??

Much appreciated
0
 
LVL 86

Expert Comment

by:jkr
ID: 18785858
No, function pointer require native Win32 modules. Since C# is a P-code language (which means that it is interpreted to some extent), there are no memory addresses that resemble assembly code which could serve as a function pointer. That's why you need P/Invoke as an intermediate layer that provides these.
0
 

Author Comment

by:maloriopolium
ID: 18786874
Can you explain what it means by using P/Invoke? It seems that all I have to do is declare the attribute:
[DllImport("user32.dll")]

and then declare a
static extern int SendMessage(....)
function which should be that of the Win32 API function.
Is that all there is to using P/Invoke?

0
 
LVL 86

Expert Comment

by:jkr
ID: 18787147
Yes, that pretty much is it. However, I'd suggest to have the DLL that exposes the callback function itself call 'SendMessage()', since you need to have a native module pass that function pointer, managed modules won't be able to get that one.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 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

17 Experts available now in Live!

Get 1:1 Help Now