简体   繁体   English

从XLL函数返回Unicode字符串

[英]Returning unicode string from XLL function

I'm write Excel 2007/2010 XLL using Excel 2007 XLL SDK in VS 2010. My function can return varios strings: 我在VS 2010中使用Excel 2007 XLL SDK编写Excel 2007/2010 XLL。我的函数可以返回varios字符串:

 __declspec(dllexport) LPXLOPER12 WINAPI PTstate (double P, double T)
{
       static XLOPER12 xResult;
       xResult.xltype = xltypeStr;  
       debugPrintf("P = %d\n", P);
       debugPrintf("T = %d\n", T);
       Calculate(P, T);
       Reg = GetRegion(DV.p, DV.t);
       xResult.val.str = L"";
       if (Reg == 0) {xResult.val.str = L"\027Ошибка диапазона параметров";debugPrintf("Reg = 0");}
       if (Reg == 1) {xResult.val.str = L"\004Вода";debugPrintf("Reg = 1");}
       if (Reg == 2) {xResult.val.str = L"\014Перегретый пар";debugPrintf("Reg = 2");}
       if (Reg == 3) {xResult.val.str = L"\017Критическая смесь";debugPrintf("Reg = 3");}
       if (Reg == 4) {xResult.val.str = L"\014Насыщенный пар";debugPrintf("Reg = 4");}
       if (Reg == 5) {xResult.val.str = L"\019Сверхперегретый пар";debugPrintf("Reg = 5");}
    if (Reg != 0) MathVater();
    return(LPXLOPER12) &xResult;
}

When string values return to excel it's value - 'Ошибка диапазона параме' - not all first string. 当字符串值恢复为excel时,它的值为-'Ошибкадиапазонапараме'-并非所有的第一个字符串。 But I'm tell length os string with "\\027" in start of string. 但是我在字符串的开头用“ \\ 027”告诉了长度os字符串。 Where I'm mistaken? 我在哪里弄错了?

Edit 1 : 编辑1

I've changed code to:
if (Reg == 0) {xResult.val.str = L"\035Ошибка диапазона параметров";debugPrintf("Reg = 0");}
       if (Reg == 1) {xResult.val.str = L"\012Вода";debugPrintf("Reg = 1");}
       if (Reg == 2) {xResult.val.str = L"\022Перегретый пар";debugPrintf("Reg = 2");}
       if (Reg == 3) {xResult.val.str = L"\025Критическая смесь";debugPrintf("Reg = 3");}
       if (Reg == 4) {xResult.val.str = L"\022Насыщенный пар";debugPrintf("Reg = 4");}
       if (Reg == 5) {xResult.val.str = L"\027Сверхперегретый пар";debugPrintf("Reg = 5");}

But I've got this: 但是我有这个:

在此处输入图片说明 What do you think about? 你有什么想法?

Edit 2 : My function signature type: 编辑2 :我的函数签名类型:

{
              L"PTstate",                 // Function name/ordinal
              L"UBB",                    // Func signature type
              L"PTstate",                 // Func name in Func wizard
              L"P, T",             // Arg name in Func wizard
              L"1",                      // Function type
              L"SimpleXll2007",           // Category in Func wizard
              L"",                       // Shortcut (commands only)
              L"",                       // Help topic
              L"",  // Func help in Func wizard
              L"",          // Arg help in Func wizard
              L""           // Arg help in Func wizard       
       }

When specifying the string length for the wide character string, the first wide character holds the length of the string. 指定宽字符串的字符串长度时,第一个宽字符将保留字符串的长度。 If it's purely cyrillic unicode this is 8 bytes, for just roman characters it'll be 4 bytes. 如果它是纯粹的西里尔unicode,则为8个字节,对于罗马字符,则为4个字节。

So this should work: 所以这应该工作:

if (Reg == 0) {xResult.val.str = L"\035Ошибка диапазона параметров";debugPrintf("Reg = 0");}

See more here (see Table 2): http://msdn.microsoft.com/en-us/library/office/aa730920%28v=office.12%29.aspx 在此处查看更多信息(请参阅表2): http : //msdn.microsoft.com/zh-cn/library/office/aa730920%28v=office.12%29.aspx

I've used char* for returning string. 我使用char *返回字符串。 Who find solution with XLOPER12 - welcome! 谁能用XLOPER12找到解决方案-欢迎光临!

As far as I can see, the problem is the lengths of the strings you have specified are not in fact Octal. 据我所知,问题在于您指定的字符串的长度实际上不是八进制的。

I have taken your code and created a simplified example, with the correct Octal lengths and it works just fine. 我已采用您的代码,并创建了一个简化的示例,具有正确的八进制长度,并且工作正常。

__declspec(dllexport) LPXLOPER12 XLLMemoryE (int Reg)  // REGISTRATION Type UH
{ 

    static XLOPER12    xResult;

    xResult.xltype = xltypeStr;                 

    if (Reg == 0) {xResult.val.str = L"\034Ошибка диапазона параметров";}
    if (Reg == 1) {xResult.val.str = L"\004Вода";}
    if (Reg == 2) {xResult.val.str = L"\015Перегретый пар";}
    if (Reg == 3) {xResult.val.str = L"\021Критическая смесь";}
    if (Reg == 4) {xResult.val.str = L"\015Насыщенный пар";}
    if (Reg == 5) {xResult.val.str = L"\023Сверхперегретый пар";}

    return &xResult;        
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM