LPTSTR 转 int (c++)
我在创建进程(在 VC++ 中)时传递了一些数字参数,但
我一直在将 LPTSTR 转换为 int。
提前致谢。
I'm passing some numeric arguments while creating a process (in VC++)
I'm stuck at converting LPTSTR to int.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
LPTSTR 只是一个指向字符或宽字符字符串的长指针。
使用
_ttoi
或_tstoi
进行与字符宽度无关的转换方式。另请参阅
A LPTSTR is just a Long Pointer to a char or wide-char string.
Use
_ttoi
or_tstoi
for a character-width-agnostic way of doing the conversion.Also see
嘎嘎! 你到底想做什么? 您的问题是在父进程端(调用
CreateProcess()
)还是在子进程端?有多种方法可以将参数从父进程传递到由 创建的子进程
CreateProcess()
函数。 如果您可以将这些参数编码为大小合理的字符串,那么通过命令行参数(CreateProcess 的 lpCommandLine 参数)传递它们可能是最直接的方法。 便携式,环境变量第二。如果您的数据结构无法编码为合理大小的字符串(因为它需要大量内存,或者它不容易序列化),那么您需要诉诸某种进程间通信的方法。 文件或共享内存块是执行此操作的两种方法。 无论哪种情况,您都需要为子进程找到一个商定的位置(如果是文件,则为文件路径,或共享内存块的名称),然后将此字符串作为命令行传递参数或环境变量。
如果您尝试在子进程中解析 lpCommandLine 参数,那么这就是其他人的建议。
Gack! What exactly are you trying to do? Is your problem on the parent process side (which calls
CreateProcess()
) or on the child process side?There are several ways of communicating parameters from a parent process to a child process created by the
CreateProcess()
function. If you can encode those parameters as a reasonably-sized string, then passing them through command-line parameters (thelpCommandLine
parameter of CreateProcess) is probably the most straightforward & portable, with environment variables 2nd.If you have a data structure you can't encode in a reasonably-sized string (either because it's a large amount of memory, or it's not easily serialized), then you need to resort to some method of interprocess communication. A file or a block of shared memory are two ways of doing this. In either case you need to come up with an agreed-upon location for the child to find this (a filepath in the case of a file, or the name of the shared memory block), and then pass this string as a command-line parameter or environment variable.
If you're trying to parse the
lpCommandLine
parameter within the child process, then it's what other people have suggested.我的建议是使用 _tcstol 而不是 _ttoi 之类的东西,这样您就可以处理错误情况,例如字符串中的非数字。 例如:
在这两种情况下结果都是 0,但只有第二种情况才会出现转换错误。
My advice would be to use something like _tcstol rather than _ttoi, so you can handle error conditions such as non-digits in the string. For instance:
In both cases the result will be 0, but only in the second case there is an error in conversion.
尝试使用
atoi()
函数(或适当的版本(如果您使用宽字符)将字符串转换为整数。Try the
atoi()
function (or the appropriate version if you're using wide characters) to convert strings to integers.LPTSTR 是一个指向字符串的指针,因此如果您想要的是表示字符串值的 int,则不应将其转换为 int。
如果您知道字符串包含数字,例如“1234”,您应该能够使用 _wtoi 函数将其转换为 int,
例如
int num = _wtoi(foo);
其中 foo 是 LPTSTR。
编辑:只有当 LPTSTR 是 UNICODE 字符串时,上述方法才能正常工作。 即_UNICODE 被定义。 如果不是,你应该使用 atoi。
请参阅 http://msdn.microsoft.com/en-我们/库/yd5xkb5c(VS.80).aspx
LPTSTR is a pointer to a string so you shouldn't cast it to an int if what you want is an int representing the value of string.
If you know that the string contains digits, e.g. "1234" you should be able to use the _wtoi function to convert it to an int
e.g.
int num = _wtoi(foo);
where foo is a LPTSTR.
edit: The above only works properly if the LPTSTR is a UNICODE string. i.e. _UNICODE is defined. If it isn't you should use atoi.
See http://msdn.microsoft.com/en-us/library/yd5xkb5c(VS.80).aspx