473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

PInvoke problem with int by reference

Hi,

I have the following PInvoke problem:

I want to call the Win32 API SystemParametersInfo()
which has the following signature:

BOOL SystemParametersInfo(
UINT uiAction,
UINT uiParam,
PVOID pvParam,
UINT fWinIni
);

the pinvoke.net site suggests the following declaration:

[DllImport("user32.dll", SetLastError = true)]
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr
pvParam, uint fWinIni);

I'm interested in the case when uiAction is set to SPI_GETSCREENSAVEACTIVE
where pvParam "must point to a BOOL variable that receives..." (BOOL=32bit
signed int)

How to pass an 32-bit integer by reference to the above signature?

Any ideas appreciated.
Nicolas
Nov 17 '05 #1
5 4259

"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> wrote in message
news:eN**************@TK2MSFTNGP10.phx.gbl...
Hi,

I have the following PInvoke problem:

I want to call the Win32 API SystemParametersInfo()
which has the following signature:

BOOL SystemParametersInfo(
UINT uiAction,
UINT uiParam,
PVOID pvParam,
UINT fWinIni
);

the pinvoke.net site suggests the following declaration:

[DllImport("user32.dll", SetLastError = true)]
static extern bool SystemParametersInfo(uint uiAction, uint uiParam,
IntPtr pvParam, uint fWinIni);

I'm interested in the case when uiAction is set to SPI_GETSCREENSAVEACTIVE
where pvParam "must point to a BOOL variable that receives..." (BOOL=32bit
signed int)

How to pass an 32-bit integer by reference to the above signature?


The signature doesn't call for uiAction by reference. It wants it by value.
And if you need to pass a signed 32-bit integer, just declare it as
accepting a signed 32-bit integer.

[DllImport("user32.dll", SetLastError = true)]
static extern bool SystemParametersInfo(int uiAction, uint uiParam, IntPtr
pvParam, uint fWinIni);

Since C is not type safe, C dll's often play fast and loose with function
declarations. That declaration really only instructs you how to put 32-bits
on the stack. It says very little about which 32 bits. That's up to you.

David
Nov 17 '05 #2
thanks for your quick reply, but the parameter I have problems with is
<pvParam>, which is IntPtr.
Any idea?

Nick
Nov 17 '05 #3

"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> wrote in message
news:O1**************@TK2MSFTNGP12.phx.gbl...
thanks for your quick reply, but the parameter I have problems with is
<pvParam>, which is IntPtr.
Any idea?

Again, declare it as you would like to use it. Just remember that you can't
change the number of bytes you put on the stack. So pvParam MUST be 4
bytes, but it needn't be an IntPtr.
you wrote: I'm interested in the case when uiAction is set to SPI_GETSCREENSAVEACTIVE
where pvParam "must point to a BOOL variable that receives..." (BOOL=32bit
signed int)


pointer to BOOL, is a pointer to an signed 32-bit integer, which you can get
by passing a ref int, instead of a IntPtr.

When you do this you should also alias the function, because you may need to
call it with different parameters

EG

//#define SPI_GETSCREENSAVEACTIVE 0x0010
const uint SPI_GETSCREENSAVEACTIVE = 0x0010;
const int TRUE = 1;
const int FALSE = 0;

[DllImport("user32.dll", SetLastError = true, EntryPoint =
"SystemParametersInfo")]
static extern int SystemParametersInfo_SPI_GETSCREENSAVEACTIVE(uint
uiAction, uint uiParam,
ref int pvParam, uint fWinIni);

static bool IsScreenSaveActive()
{
int bActive = -1;
int rc =
SystemParametersInfo_SPI_GETSCREENSAVEACTIVE(SPI_G ETSCREENSAVEACTIVE, 0, ref
bActive, 0);
if (rc == 0)
{
throw new ApplicationException("SystemParametersInfo failed");
}
return (bActive == TRUE);
}

David
Nov 17 '05 #4
thanks! That helps a lot.
Nick
Nov 17 '05 #5
check out www.pinvoke.net for more info on interop

HTH

Ollie Riches

"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
thanks! That helps a lot.
Nick

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RobJUK66 | last post by:
have a 3rd party .Net dll that wraps a set of further unmanaged dll's. The ..Net DLL basically uses pinvoke to wrap the calls. The low level dlls provide an API to talk to a complex device which...
3
by: Brett Robichaud | last post by:
I have created a simple background thread to make one pinvoke call into a DLL I've created. My Winforms app spawns the thread in the form load event then go about it's business. The problem is...
11
by: Joe Martin | last post by:
Has anyone made use of PKWare's PKCDL.DLL for the Implode and Explode functions? I have been able to successfully define everything correctly and even get the invoked function (Explode) to run...
5
by: vertigo | last post by:
Hello I use some win 32 API function for example: HANDLE CreateFile( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD...
0
by: Dgdege | last post by:
Hi all, I want to wrapp an exiting API written in C++ to .NET using managed C++ extensions. I want my managed C++ class library to link with the unmanaged dll. So I don't use PInvoke for...
0
by: Petar Popara | last post by:
I've got this error calling one DLL func from my VB.net app: An unhandled exception of type 'System.Runtime.InteropServices.MarshalDirectiveException' occurred in Test.exe Additional...
6
by: Pucca | last post by:
I have a program that originally compiles into a exe file. I changed the compile option to generate dll file. This program calls a com component. Can I use pinvoke in C# to call it? The...
3
by: angshuman.agarwal | last post by:
Structure in C DLL ---------------------------- typedef struct IrData { unsigned short uiFormat; unsigned short uiLength; unsigned char* pchData; } tagIrData; Function in C DLL
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.