Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

LVM_SORTITEMS

Posted on 2007-03-22
11
913 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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