Solved

LVM_SORTITEMS

Posted on 2007-03-22
11
902 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

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

15 Experts available now in Live!

Get 1:1 Help Now