|
|
I had wrote a code as follows below, is it works fine ?
<br />
void write( char *format, ... )<br />
{<br />
va_list args;<br />
char buffer[1025];<br />
va_start( args, format );<br />
vsprintf( buffer, format, args );<br />
va_end(args);<br />
printf( buffer );<br />
}<br />
<br />
<br />
#define txtmsg(x) write(x)<br />
<br />
int tmpval = 100;<br />
txtmsg("tempvalue = %d", tmpval );<br />
but i am not getting a correct answer
kindly help me
-kk.tvm-
|
|
|
|
|
Depends on what it is suposed to do, really...however, you declared your txtmsg macro here:
kk.tvm wrote: #define txtmsg(x) write(x)
to take 1 parameter (x), but here:
kk.tvm wrote: txtmsg("tempvalue = %d", tmpval );
you used it with two ("tempvalue = %d" is one and tmpval is two), the compiler won't like that.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
|
Hope u want 100 to printed, right?
but in your case
u gave a wrong input to txtmsg, right?
check?
You gave it as "tempvalue = %d", tmpval to a macro,
remember, a macro never have any error checking, since it is already expanded on preprocessing time.
it will take the value for x as "tempvalue = %d", tmpval
so are getting a wrong output.
Please give correct input for your macro txtmsg like given below
int tmpval = 100;
char buff[10];
itoa( tmpval, buff, 10 );
txtmsg( buff );
Величие не Бога может быть недооценена.
|
|
|
|
|
thanks for your solution
-kk.tvm-
|
|
|
|
|
You can't have variadic macros (macros with varying numbers of arguments) in C++. You can in C99, but not in standard C++.
So, change your #define to:
#define txtmsg write
and leave argument handling to the function rather than trying to do something with it in hte macro.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
thanks for valuable reply
-kk.tvm-
|
|
|
|
|
kk.tvm wrote: I had wrote a code as follows below, is it works fine ?
Wouldn't that be your question to answer?
To solve this problem yourself, compile the code using the /P switch. Then look in the .i file to see how that txtmsg macro was expanded.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I am implementing a C++.Net Class Library.(I want to use it in C#.Net later) I am using an MFC C++ header file and its corresponding lib file in that class library. I set the path for header file and lib file in the project properties. In that header file there is a class which I have no problem using it. But there is an "extern "C"" function which I have some problem with it. When I call it in my code, there are two link errors while compiling:
error LNK2028
error LNK2019
Please help me to use this function. I'd appreciate it.
Thanks in advance
|
|
|
|
|
You have to specify the calling convention of the exported function because the default is different in managed and native compilation.
You could declare it as __stdcall.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
would you please elaborate a little bit more or send me an article with examples?
Thanks a lot
|
|
|
|
|
The function must be declared as -
extern "C" int __stdcall foo();
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Here[^] is a great article about calling conventions. It will help you to understand the concept.
|
|
|
|
|
Hi,
I want to know the side effects if I don't save the value returned from a function call.
ex:
int foo()
{
return (10);
}
main()
{
foo();
}
Doubts:
1. Does this type of code may cause some warnings or other issues?
2. Is it an optimized approach or do I need to save the value returned by foo()?
Thanks for ur help.
Akash
|
|
|
|
|
Ideally, you should take the returned value because there is some reason for the returning it.
Could be an error code that most people don't check.
As for optimization, you will have one less instruction if you don't take the return value.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
yaa, true...
But if I've already checked for error conditions and all. does it matter...
|
|
|
|
|
It really doesn't matter.
For example, I didn't know printf returned any value at all for a long time.
I believe its return value is not checked 99% of the times.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
akashag wrote: 1. Does this type of code may cause some warnings or other issues?
Compile it and see...neither gcc nor VC++ seem to give any warnings.
akashag wrote: 2. Is it an optimized approach or do I need to save the value returned by foo()?
You don't need to save the return value...but you then have to ask yourself - why am I calling the function? Doers it have other, non-visible side-effects? Obviously, this simple case doesn't, but many functions do effect some form of global data, while class methods can obviously update object instance data.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I can discover from the trusty old DOS functions on which logical drive I currently am say, drive 'C:', or drive 2. logical drive typically represents a volume on a physical drive, say '\\\\.\\PhysicalDrive0' What API's are there to translate from a locical drive identifier to the physical drive which contains the volume.
regards
|
|
|
|
|
You could use the IOCTL_STORAGE_GET_DEVICE_NUMBER[^] control code to obtain the physical drive and volume number.
For example:
#include <Winioctl.h>
VOID LogicalToPhysical(TCHAR *szDrive)
{
CString szPhysical;
HANDLE h = CreateFile(szDrive, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
if(INVALID_HANDLE_VALUE != h)
{
STORAGE_DEVICE_NUMBER sd;
DWORD dwRet;
if(DeviceIoControl(h, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sd, sizeof(STORAGE_DEVICE_NUMBER), &dwRet, NULL))
{
szPhysical.Format(_T("\\\\.\\PhysicalDrive%d"), sd.DeviceNumber);
}
CloseHandle(h);
}
}
LogicalToPhysical(_T("\\\\.\\\\C:"));
Note that this control code will fail to return correct information for mirrored and striped volumes. A more complete solution for these types of drives would be the IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS[^] control code which will return an array of partitions for the drive.
Best Wishes,
-David Delaune
|
|
|
|
|
Thanks David,
Exactly the hint I Needed.
Only One Snag, Cannot find the (hex)Value of IOCTL_STORAGE_GET_DEVICE_NUMBER. Could you let me know?
Thanks and Regards,
Bram van Kampen
modified on Tuesday, August 11, 2009 5:17 PM
|
|
|
|
|
Bram van Kampen wrote: Cannot find the (hex)Value of IOCTL_STORAGE_GET_DEVICE_NUMBER.
Interesting. They are declared in the Winioctl.h header file.
Some questions:
1.) What development environment are you using?
2.) What version of the Platform/Windows SDK are you using if any?
You can probably get away with dropping these declarations somewhere:
#define FILE_ANY_ACCESS 0
#define FILE_SPECIAL_ACCESS (FILE_ANY_ACCESS)
#define FILE_READ_ACCESS ( 0x0001 ) // file & pipe
#define FILE_WRITE_ACCESS ( 0x0002 ) // file & pipe
#define METHOD_BUFFERED 0
#define METHOD_IN_DIRECT 1
#define METHOD_OUT_DIRECT 2
#define METHOD_NEITHER 3
#define FILE_DEVICE_MASS_STORAGE 0x0000002d
#define IOCTL_STORAGE_BASE FILE_DEVICE_MASS_STORAGE
#define CTL_CODE( DeviceType, Function, Method, Access ) ( \
((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \
)
#define IOCTL_STORAGE_GET_DEVICE_NUMBER CTL_CODE(IOCTL_STORAGE_BASE, 0x0420, METHOD_BUFFERED, FILE_ANY_ACCESS)
typedef struct _STORAGE_DEVICE_NUMBER {
DEVICE_TYPE DeviceType;
DWORD DeviceNumber;
DWORD PartitionNumber;
} STORAGE_DEVICE_NUMBER, *PSTORAGE_DEVICE_NUMBER;
You should really correctly setup your development environment rather than dropping the above declarations into your project. Are you using VC6 or something?
Best Wishes,
-David Delaune
|
|
|
|
|
|
Many thanks, that solved the problem. I had looked at DeviceIoControl() but my (outdated) Winioctl.h did not have is call param!
Regards
Andy
|
|
|
|