How to use the ListView_GetBkImage macro in C# - c#

How can I use the ListView_GetBkImage macro:
http://msdn.microsoft.com/en-us/library/bb761246(v=VS.85).aspx
... from a C#/WinForms application? I think this macro just wraps the SendMessage method, but I'm not sure. I couldn't find any C#-based samples on this.
Basically I'm trying to get a LVBKIMAGE ( http://msdn.microsoft.com/en-us/library/bb774742(v=VS.85).aspx ) structure that references the Desktop's background bitmap.

You cannot. A macro is processed at compile time by the C/C++ compiler, but you want to access the binary library. You will just have to find the macro in the source, see what it does and do the same in your C# code. It shouldn't be anything too complex. Download the Platform SDK if you don't already have it and look in the .h file mentioned in the documentation.
Edit: OK, so the macro is defined as:
#define ListView_GetBkImage(hwnd, plvbki) \
(BOOL)SNDMSG((hwnd), LVM_GETBKIMAGE, 0, (LPARAM)(plvbki))
SNDMSG is simply defined as SendMessage. LVM_GETBKIMAGE is an integer - it's 0x1045 for the ASCII version and 0x108B for the Unicode version. (You probably want the Unicode version if you're unsure.) So the entire thing resolves to:
(BOOL)SendMessage(hwnd, 0x108B, 0, plvbki)
There should be easy enough to map to C#. Look in System.Windows.Forms using Reflector to see how Microsoft have imported the SendMessage function. It will be marked internal, so you cannot call it, but you can copy it. plvbki is a pointer to a struct - you'll need to create a C# equivalent of LVBKIMAGE. Actually, MS have probably done that for you too, so look around for that.

Related

Calling StartXpsPrintJob1()

I'm trying to use the StartXpsPrintJob1 API in C#.
However, I couldn't find any information about it using Google. This method is not listed on pinvoke.net.
Seems I have to translate the C++ code, including classes and interfaces, into C# and use some kind of dllimport? Unfortunately, I have zero knowledge about C++.
How can I acheive this?
Seems I have to translate the C++ code, including classes and interfaces, into C# and use some kind of dllimport?
You can save time and effort and use a tool named SWIG, (Sourceforge project page) to generate c# code that generate dllimport.
How to use:
Download SWig for windows
Create c++ header file that represent StartXpsPrintJob1 , name the file xpsheader.h
create interface file e.g example.i
%module example
%{
/* Includes the header in the wrapper code */
#include "xpsheader.h"
%}
/* Parse the header file to generate wrappers */
%include "xpsheader.h"
Run the command:
swig -csharp example.i
The tool generate punch of files with one file named examplePINVOKE.cs
In fact, it is important to know that SWIG is a fairly complete C++ compiler with support for nearly every language feature. This includes preprocessing, pointers, classes, inheritance, and even C++ templates. SWIG can also be used to package structures and classes into proxy classes in the c# target language---exposing the underlying functionality in a very natural manner.
The error "invalid assembly" is would probably occur if you tried to import XpsPrint.dll into your project as a .Net library: it's actually a "native code" library :(
In theory, you should be able to copy/paste this example into a new MSVS/C# project:
How to: Programmatically Print XPS Files.
You'll notice that it just "prints" - it doesn't use any XPS-specific APIs.
If that doesn't work for you, this link might also help:
Programmatically print an XPS file to a physical printer
Finally, please note in the Microsoft documentation:
https://msdn.microsoft.com/en-us/library/windows/desktop/ff686814(v=vs.85).aspx
[The XPS Print API is not supported and may be altered or unavailable
in the future. Client applications should use the Print Document
Package API instead.]

Matlab output to C#

I have a complicated Matlab function which I would not like to re-write in C#. The function returns an array of N double-precision numbers.
Given that I have compiled the function to into a .NET assembly (a .dll file), and that the function's signature goes like [resutls] = myFunc('stringInput'), how can I call my function inside a C# code?
Thanks!
Here you can find the steps to do that:
https://www.mathworks.com/help/compiler_sdk/gs/create-a-cc-application-with-matlab-code-1.html
Is necessary have the runtime library installed on the computer that run your code (you can add it when create the .dll package)
The way I did it, is by adding MLApp as a project reference.
From MATLAB, you need to start the automation service:
enableservice('AutomationServer', true);
And within C# you can connect to Matlab using.
MLApp.DIMLApp matlabInstance = (MLApp.DIMLApp)Marshal.GetActiveObject("Matlab.Desktop.Application");
You can then use the interface functions of MLApp to interact. E.g.
int a = (int)matlabInstance.GetVariable("variableName", "base");
Or even execute stuff. E.g.:
matlabInstance.Execute("evalin( 'base' , 'plot( range , dataVector , ''k'');' );");
of course you need some error handling, etc. Normal application stuff.
There's a topic about it here

How to create a .lib file from a C# DLL?

I need to create a .lib file from a C# DLL (I think it is C# becuase of this code which calls to the dll https://code.google.com/p/thunder-missile-api/downloads/detail?name=MissileLauncher.cs&can=2&q=) In other word's I need to create a .lib for DreamCheeky Thunder Missile Launcher DLL, which comes with their software .
Now, what I need to do? I need to operate this device using C++. Easiest way is using their own DLL. The above linked code does it in C#.
I tried importing the DLL file into the project C++, but it seems like some methods are missing, specially methods like moveMissileLauncher() which are called in the C# code.
And the best thing is, I might want to move to QT (most probably) so you know, having a .lib is a good idea.
Crating a lib from a managed dll will do no good. You need to use interop
http://msdn.microsoft.com/en-us/library/ms973872.aspx
In your specific case, I would write a C++ lib that exposes the methods you need/want to call and forwards them to the managed C# dll using interop
I would do that in C++/CLI, personally.
A good, more recent article on the options you have is here http://msdn.microsoft.com/en-us/magazine/dd315414.aspx, or look here on SO for COM/.NET interop and you will find plenty of answers.
You want to call managed C# code from your c++ application. Here is tutorial to make someway to call c# code from your c++ application, i have used this method before, and works fine for me.

Accessing Windows API Constants and Structs for P/Invoke

Quick question: How can I access the BN_CLICKED constant and other constants defined for the Win32 API from .NET? Are they defined in some library? Do I have to define them myself? If so, where can I find these values? And are the values version-specific between versions of Windows?
I find the PInvoke Interop Assistant to be really helpful:
http://blogs.microsoft.co.il/blogs/sasha/archive/2008/01/12/p-invoke-signature-generator.aspx.
It has almost everything and can convert the C++ to C#/VB for you. I rarely, if ever, resort to searching google/pinvoke.net anymore.
Here's the MSDN Magazine Article: http://msdn.microsoft.com/en-us/magazine/cc164193.aspx
The original January 2008 MSDN Magazine Article is now only available as a .CHM help file download, linked from the very bottom of https://msdn.microsoft.com/magazine/msdn-magazine-issues. (Column "CLR Inside Out: Marshaling between managed and unmanaged code.")
And here's the download: http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a0608523/CLRInsideOut2008_01.exe. The source code can be found at http://clrinterop.codeplex.com/.
You could download the Microsoft Platform SDK and take a look at the header files (*.h). E.g. the BN_CLICKED is defined in the winuser.h file.
Usually, if you just need one or two constants, a Google search and a look at the first few results is also sufficient, since the value is printed there.
http://pinvoke.net/ is an excellent resource for this many common P/Invoke definitions.
The MagNumDB website (by SO user Simon Mourier) is an easy way to look up constants:
http://www.magnumdb.com/search?q=BN_CLICKED
It's kind of a proof-of-concept, but I put together a script that can look up most any Windows API constant. Example usage:
PS > .\Get-WindowsSDKConstant.ps1 BN_CLICKED
0
PS > .\Get-WindowsSDKConstant.ps1 BN_DBLCLK
5
PS > .\Get-WindowsSDKConstant.ps1 WM_COMMAND
273
It requires you to download Visual Studio and the Windows 10 SDK, because behind-the-scenes it compiles a program that looks up the constant.
Finally, here's some answers to the asker's questions:
Are [the constants] defined in some library?
The authoritative source is the Windows Platform SDK
Do I have to define them myself?
They're not built-in to Windows or .NET, which means you'll probably define them yourself (or copy them from somewhere).
And are the values version-specific between versions of Windows?
They're very stable, because otherwise a program compiled for one version of Windows might stop working when a user upgrades to a newer version of Windows. Microsoft goes to great lengths to prevent this from happening.
However, I've seen at least one place where the constants are different depending on what platform/architecture you're compiling on. I wouldn't assume that just because your code works on x86 64-bit Windows, it'll work on ARM 32-bit Windows RT, for example.

Viewing Contents Of a DLL File

is this possible to view contents and Functions of a DLL file...
few times ago i was playing with OlyDBG then i found there is option for viewing contents of dll...
so suggest me any good tool or soft for this...
and suppose i have a DLL named "Python27.dll"...
now i need to view the content of this DLL so what do i do...
thanx...
While not trivial to use (you need to understand the format of a Portable Executable, aka PE, file), pefile seems a good, powerful and versatile tool for the purpose of viewing a DLL or any other PE file (I wouldn't risk using it to change such a file, although I see it's one of its features).
For example, excerpting the module's usage examples (and editing to show a dll instead of the equally hypothetical filename they use, which is an exe;-):
import pefile
pe = pefile.PE(‘/path/to/pefile.dll’)
for exp in pe.DIRECTORY_ENTRY_EXPORT.symbols:
print hex(pe.OPTIONAL_HEADER.ImageBase + exp.address), exp.name, exp.ordinal
should, according to the wikipage I pointed to, display something like:
0x7ca0ab4f SHUpdateRecycleBinIcon 336
0x7cab44c0 SHValidateUNC 173
0x7ca7b0aa SheChangeDirA 337
0x7ca7b665 SheChangeDirExA 338
0x7ca7b3e1 SheChangeDirExW 339
0x7ca7aec6 SheChangeDirW 340
0x7ca8baae SheConvertPathW 341
Dependency Walker may provide what you want/need -- it certainly shows all the entry points in a DLL.
On Windows, DUMPBIN provides some DLL inspection capabilities. For example:
DUMPBIN /EXPORTS C:\path\to\my.dll
will display all the exported definitions.
I've done some work with ctypes, and loading dlls in windows, but I don't think DLL have any sort of introspection. This really isn't a big deal, because all of the function calls in DLLs are static. If your trying to use a undocumented DLL, you would not only need to know the names of the functions, but also the parameters of the functions. You would have to reverse engineer the DLL, no small task.
So, in my opinion, I would say no.

Categories

Resources