Excel偶尔会将垃圾字符传递给我的C API UDF [英] Excel occasionally passing junk characters to my C API UDF

查看:62
本文介绍了Excel偶尔会将垃圾字符传递给我的C API UDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用C API开发一个加载项,下面的代码是我编写的异步UDF的片段。 函数ppkw使用类型字符串"> XQ"注册,这意味着第二个参数应该是通用LPXLOPER12
,表示在Excel中传递给ppkw_v()的单个参数。


这段代码的目的是将ppkw_v()的用户提供的参数强制转换为CHAR *字符串,并将其传递给代码的其余部分。 大约98%的时间它没有问题,但有时候额外的字符在"deckstring"结尾处弹出
。 CHAR数组。


确信这是我的变量被覆盖的问题,或者切断了字符串的空终止符,我开始在函数的各个阶段记录字符串,并发现在这种情况下这些额外的字符会弹出,当它传递给函数时,它们在deckInput.val.str中出现


例如,输入= ppkw_v(" foo" )进入一个单元格并将其拖动超过200行然后检查日志显示大多数运行正确记录"foo"作为字符串内容,但偶尔会出现像"foo.d"这样的字符串。或"foo"r"会弹出


我是否遗漏了有关如何从deckInput中提取字符串内容的内容?

 void WINAPI ppkw_v(LPXLOPER12 xlAsyncHandle,LPXLOPER12 deckInput)
{
FILE * fp = fopen(" xlcanary.txt"," a");
fputs("Async UDF Called\ n",fp);
fflush(fp);

fputs("Raw Excel String",fp);
fputws(deckInput-> val.str,fp); //< - 此日志记录语句显示额外的字符!
fputs(" \ n",fp);
fflush(fp);

LPXLOPER12 xlHandle = TempOper12(xlAsyncHandle);
XLOPER12 strInput,destType;

destType.xltype = xltypeInt;
destType.val.w = xltypeStr;

Excel12(xlCoerce,& strInput,2,deckInput,& destType);

fputs("Coerced Excel String",fp);
fputws(strInput.val.str,fp);
fputs(" \ n",fp);
fflush(fp);

char strBuffer [400] = {0};
wcstombs(strBuffer,strInput.val.str + 1,wcslen(strInput.val.str)-1); //切掉前导字符Excel添加

fputs(" String Converted\\\
",fp);
fflush(fp);

char * deckString = new char [strlen(strBuffer)+1]();

fputs("New Char Allocated\\\
",fp);
fflush(fp);

strcpy(deckString,strBuffer);

fputs(" Copied Char String",fp);
fputs(deckString,fp);
fputs(" \ n",fp);
fflush(fp);

解决方案

< blockquote>发布后,我查看了TempStr12()的示例代码,并注意到如何使用xloper12.val.str的第一个字符来编码字符串长度。 我认为这意味着默认情况下,在
val.str结尾处没有终止空字符,因为Excel会将其交给您,或者您不能依赖于它。或者您不能依赖它。我重新编写了我的字符串转换代码来键入val.str [0]而不是wcslen()的最终字符串长度,它现在似乎可以工作。


I've been developing an add-in using the C API, and the below code is a snippet of an asynchronous UDF that I've written.  The function ppkw is registered with the type string ">XQ", meaning the second argument should be a generic LPXLOPER12 representing the single argument passed to ppkw_v() in Excel.

The purpose of this section of code is to coerce the user-provided argument to ppkw_v() to a CHAR* string and pass it along to the rest of the code.  Approximately 98% of the time it works without issue, but from time to time extra characters were popping up on the end of the "deckstring" CHAR array.

Convinced this was a problem with my variables being overwritten, or chopping off a string's null terminator, I started logging the string at various stages of the function and found that in cases where these extra characters are popping up, they were present in deckInput.val.str when it was passed to the function.

For example, entering =ppkw_v("foo") into a cell and dragging it down over 200 rows and then checking the log shows the majority of runs correctly log "foo" as the string content, but occasionally strings like "foo.d" or "foo)r" would pop up.

Am I missing something about how to extract the string content from deckInput?

void WINAPI ppkw_v(LPXLOPER12 xlAsyncHandle, LPXLOPER12 deckInput)
{
	FILE *fp = fopen("xlcanary.txt", "a");
	fputs("Async UDF Called\n", fp);
	fflush(fp);

	fputs("Raw Excel String ", fp);
	fputws(deckInput->val.str, fp); // <- This logging statement shows extra chars!
	fputs("\n", fp);
	fflush(fp);

	LPXLOPER12 xlHandle = TempOper12(xlAsyncHandle);
	XLOPER12 strInput, destType;

	destType.xltype = xltypeInt;
	destType.val.w = xltypeStr;

	Excel12(xlCoerce, &strInput,2, deckInput, &destType);
	
	fputs("Coerced Excel String ", fp);
	fputws(strInput.val.str, fp);
	fputs("\n", fp);
	fflush(fp);

	char strBuffer[400] = { 0 };
	wcstombs(strBuffer, strInput.val.str+1, wcslen(strInput.val.str)-1); //Chop off the leading char Excel adds

	fputs("String Converted\n", fp);
	fflush(fp);

	char *deckString = new char[strlen(strBuffer)+1]();
	
	fputs("New Char Allocated\n", fp);
	fflush(fp);

	strcpy(deckString, strBuffer);

	fputs("Copied Char String ", fp);
	fputs(deckString, fp);
	fputs("\n", fp);
	fflush(fp);

解决方案

After posting this, I reviewed the sample code for TempStr12() and noticed how the first character of xloper12.val.str is used to encode the string length.  I take this to mean that either there is by default no terminating null char at the end of val.str as Excel hands it to you, or you can't depend on it sticking around.  I re-worked my string conversion code to key the final string length off val.str[0] rather than wcslen(), and it now seems to work.


这篇关于Excel偶尔会将垃圾字符传递给我的C API UDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆