Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;命令行参数';s值大小限制_C++_Windows_Command Line_Clipboard - Fatal编程技术网

C++ C++;命令行参数';s值大小限制

C++ C++;命令行参数';s值大小限制,c++,windows,command-line,clipboard,C++,Windows,Command Line,Clipboard,我有以下代码可以在Windows7中将命令行参数的值获取到剪贴板。我使用奥威尔DeV C++编译和运行我的脚本。 //#include "stdafx.h" #include "windows.h" #include "string.h" #include <direct.h> #include <iostream> int main(int argc, wchar_t* argv[]) { LPWSTR cwdBuffer; // Get the c

我有以下代码可以在Windows7中将命令行参数的值获取到剪贴板。我使用奥威尔DeV C++编译和运行我的脚本。
//#include "stdafx.h"
#include "windows.h"
#include "string.h"
#include <direct.h>
#include <iostream>

int main(int argc, wchar_t* argv[])
{
    LPWSTR cwdBuffer;

    // Get the current working directory:
    if( (cwdBuffer = _wgetcwd( NULL, 0 )) == NULL )
        return 1;

    //DWORD len = wcslen(cwdBuffer);
    DWORD len = wcslen(argv[0]);
    HGLOBAL hdst;
    LPWSTR dst;

    // Allocate string for cwd
    hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
    dst = (LPWSTR)GlobalLock(hdst);
    memcpy(dst, cwdBuffer, len * sizeof(WCHAR));
    dst[len] = 0;
    GlobalUnlock(hdst);

    // Set clipboard data
    if (!OpenClipboard(NULL)) return GetLastError();
    EmptyClipboard();
    if (!SetClipboardData(CF_UNICODETEXT, hdst)) return GetLastError();
    CloseClipboard();

    free(cwdBuffer);
    return 0;
}
/#包括“stdafx.h”
#包括“windows.h”
#包括“string.h”
#包括
#包括
int main(int argc,wchar_t*argv[])
{
LPWSTR cwdBuffer;
//获取当前工作目录:
if((cwdBuffer=_wgetcwd(NULL,0))==NULL)
返回1;
//DWORD len=wcslen(cwdBuffer);
DWORD len=wcslen(argv[0]);
HGLOBAL-hdst;
LPWSTR dst;
//为cwd分配字符串
hdst=全球ALLOC(GMEM_可移动| GMEM_数据共享,(len+1)*大小(WCHAR));
dst=(LPWSTR)全局时钟(hdst);
memcpy(dst、cwdBuffer、len*sizeof(WCHAR));
dst[len]=0;
GlobalUnlock(hdst);
//设置剪贴板数据
如果(!OpenClipboard(NULL))返回GetLastError();
清空电路板();
如果(!SetClipboardData(CF_UNICODETEXT,hdst))返回GetLastError();
CloseClipboard();
免费(cwdBuffer);
返回0;
}
所以我已经编译并运行了一次这个脚本,并在这个过程中生成了它的可执行文件,这样我就可以运行这个可执行文件(可能作为命令分配给像geany这样的任何编辑器,以获取它当前打开的文件路径到剪贴板),并传递参数(%d,如果是geany),然后将它的值传递到剪贴板

但是当我的命令行参数的值像
C:\wamp\www\magento1922\app\code\community\Cm\RedisSession\Model
时,它只将
C:\wamp\www\magento1922\app\code\commu
复制到剪贴板


为什么呢?它与windows命令行参数大小限制有关吗?

感谢所有的评论,他们为我指出了正确的方向,我能够使脚本完全正常工作

我删除了
cwdBuffer
的不必要引用,并将其替换为参数数组元素
argv[0]
。下面是代码

#include "windows.h"
#include "string.h"
#include <direct.h>
#include <iostream>
#include <shellapi.h>
#include <stdio.h>

int main(int argc, wchar_t* argv[])
{
    //LPWSTR cwdBuffer;

    // Get the current working directory:
    if( (argv[0] = _wgetcwd( NULL, 0 )) == NULL )
        return 1;

    DWORD len = wcslen(argv[0]);
    HGLOBAL hdst;
    LPWSTR dst;

    // Allocate string for cwd
    hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
    dst = (LPWSTR)GlobalLock(hdst);
    memcpy(dst, argv[0], len * sizeof(WCHAR));
    dst[len] = 0;
    GlobalUnlock(hdst);

    // Set clipboard data
    if (!OpenClipboard(NULL)) return GetLastError();
    EmptyClipboard();
    if (!SetClipboardData(CF_UNICODETEXT, hdst)) return GetLastError();
    CloseClipboard();

    free(argv[0]);
    return 0;
}
#包括“windows.h”
#包括“string.h”
#包括
#包括
#包括
#包括
int main(int argc,wchar_t*argv[])
{
//LPWSTR cwdBuffer;
//获取当前工作目录:
如果((argv[0]=\u wgetcwd(NULL,0))==NULL)
返回1;
DWORD len=wcslen(argv[0]);
HGLOBAL-hdst;
LPWSTR dst;
//为cwd分配字符串
hdst=全球ALLOC(GMEM_可移动| GMEM_数据共享,(len+1)*大小(WCHAR));
dst=(LPWSTR)全局时钟(hdst);
memcpy(dst,argv[0],len*sizeof(WCHAR));
dst[len]=0;
GlobalUnlock(hdst);
//设置剪贴板数据
如果(!OpenClipboard(NULL))返回GetLastError();
清空电路板();
如果(!SetClipboardData(CF_UNICODETEXT,hdst))返回GetLastError();
CloseClipboard();
自由(argv[0]);
返回0;
}

传递到
argc
/
argv
的任何内容都取决于编译器的实现。在调用或不使用
argv
时,您是否看到相同的行为?当你在调试器中检查它时,
argv[0]
的值是多少?你是在混合苹果和橙子:
cwdBuffer=\u wgetcwd
len=wcslen(argv[0])
@Dieter Lucking,对不起,如果我的Win-cpp知识很基本,但是这些立场是相互关联的吗?这段代码是我从谷歌上选来的,所以我不知道哪一行几乎不需要,我尝试了长路径,但大多数路径都被完整复制,只有一些路径没有完全复制,这可能是Unicode字符串问题吗?我确实收到了一条警告,因为“int main(int,wchar\u t**)”的第二个参数应该是“char**”[-Wmain],但是如果我像往常一样使用
char
type,它会给出错误。当前工作目录和命令行是两条不相关的信息。您正在检索第一个命令行参数的长度,并将当前工作目录截断为尽可能多的字符。删除
cwdBuffer
并用
argv[0]
替换所有出现的内容“我能够使脚本完全工作”-不,不是真的。您仍然痴迷于使用当前的工作目录。当前工作目录完全无用,不应用于任何用途。如果要访问命令行,请执行
argc
/
argv
。现在还不清楚你到底想要实现什么。无论如何,这个答案不能解决任何实际的编程问题。