通过对 dll 的 stdcall 操作字符串时出现问题

发布于 2024-12-16 11:03:18 字数 2065 浏览 1 评论 0原文

我需要创建一个 C++ dll,它将通过 stdcall 从另一个程序调用。

需要什么:调用程序将向 dll 传递一个字符串数组,并且 dll 应该更改数组中的字符串值。然后,调用程序将继续使用来自 dll 的这些字符串值。

我做了一个简单的测试项目,但我显然遗漏了一些东西...

这是我的测试 C++ dll :

#ifndef _DLL_H_
#define _DLL_H_

#include <string>
#include <iostream>

struct strStruct
{
    int len;
    char* string;
};

__declspec (dllexport) int __stdcall TestFunction(strStruct* s)
{
   std::cout << "Just got in dll" << std::endl;

   std::cout << s[0].string << std::endl;
   //////std::cout << s[1].string << std::endl;

   /*
   char str1[] = "foo";
   strcpy(s[0].string, str1);
   s[0].len = 3;

   char str2[] = "foobar";
   strcpy(s[1].string, str2);
   s[1].len = 6;
   */

   //std::cout << s[0].string << std::endl;
   //std::cout << s[1].string << std::endl;

   std::cout << "Getting out of dll" << std::endl;

   return 1;
}

#endif

这是一个简单的 C# 程序,我用它来测试我的测试 dll :

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Runtime.InteropServices;

 namespace TestStr
 {
     class Program
     {
         [DllImport("TestStrLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
         public static extern int TestFunction(string[] s);

         static void Main(string[] args)
         {
             string[] test = new string[2] { "a1a1a1a1a1", "b2b2b2b2b2" };

             Console.WriteLine(test[0]);
             Console.WriteLine(test[1]);

             TestFunction(test);

             Console.WriteLine(test[0]);
             Console.WriteLine(test[1]);

             Console.ReadLine();
         }
     }
 }

这是生成的输出:

a1a1a1a1a1
b2b2b2b2b2
Just got in dll
b2b2b2b2b2
Getting out of dll
a1a1a1a1a1
b2b2b2b2b2

我有一些问题:

1) 为什么它输出数组第二个位置的元素而不是第一个位置的元素?

2)如果我取消注释dll文件中用//////注释的行,程序就会崩溃。为什么?

3)显然我想在dll中做更多的事情(/* */中的部分)比现在做的更多,但我被前两个问题阻止了......

感谢您的帮助

I need to create a C++ dll that will be called from another program through stdcall.

What is needed : the caller program will pass an array of string to the dll and the dll should change the string values in the array. The caller program will then continue to work with these string values that came from the dll.

I made a simple test project and I am obviously missing something...

Here is my test C++ dll :

#ifndef _DLL_H_
#define _DLL_H_

#include <string>
#include <iostream>

struct strStruct
{
    int len;
    char* string;
};

__declspec (dllexport) int __stdcall TestFunction(strStruct* s)
{
   std::cout << "Just got in dll" << std::endl;

   std::cout << s[0].string << std::endl;
   //////std::cout << s[1].string << std::endl;

   /*
   char str1[] = "foo";
   strcpy(s[0].string, str1);
   s[0].len = 3;

   char str2[] = "foobar";
   strcpy(s[1].string, str2);
   s[1].len = 6;
   */

   //std::cout << s[0].string << std::endl;
   //std::cout << s[1].string << std::endl;

   std::cout << "Getting out of dll" << std::endl;

   return 1;
}

#endif

and here is a simple C# program that I am using to test my test dll :

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Runtime.InteropServices;

 namespace TestStr
 {
     class Program
     {
         [DllImport("TestStrLib.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
         public static extern int TestFunction(string[] s);

         static void Main(string[] args)
         {
             string[] test = new string[2] { "a1a1a1a1a1", "b2b2b2b2b2" };

             Console.WriteLine(test[0]);
             Console.WriteLine(test[1]);

             TestFunction(test);

             Console.WriteLine(test[0]);
             Console.WriteLine(test[1]);

             Console.ReadLine();
         }
     }
 }

And here is the output produced :

a1a1a1a1a1
b2b2b2b2b2
Just got in dll
b2b2b2b2b2
Getting out of dll
a1a1a1a1a1
b2b2b2b2b2

I have some questions :

1) Why is it outputting the element in the second position of the array rather than in the first position??

2) If I uncomment the line commented with ////// in the dll file, the program crashes. Why?

3) Obviously I wanted to do more things in the dll (the parts in /* */) than what it does right now, but I am blocked by the first 2 questions...

Thanks for all your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风为裳 2024-12-23 11:03:18

您无法将字符串[]封送为本机结构

    [DllImport("TestStrLib.dll", CharSet = CharSet.Ansi, 
             CallingConvention = CallingConvention.StdCall)]
             public static extern int TestFunction(string[] s);

      struct strStruct
        {
            int len;
            char* string;
        }

    __declspec (dllexport) int __stdcall TestFunction(strStruct* s);

请阅读http://msdn.microsoft.com/en-us/library/fzhhdwae.aspx 用于各种类型的编组。

在 C# 中

    [DllImport( "TestStrLib.dll" )]
    public static extern int TestFunction([In, Out] string[] stringArray
    , int size);

在 C++ 中

__declspec(dllexport) int TestFunction( char* ppStrArray[], int size)
   {
       return 0;
   }

You cannot marshal a string[] as a native struct

    [DllImport("TestStrLib.dll", CharSet = CharSet.Ansi, 
             CallingConvention = CallingConvention.StdCall)]
             public static extern int TestFunction(string[] s);

      struct strStruct
        {
            int len;
            char* string;
        }

    __declspec (dllexport) int __stdcall TestFunction(strStruct* s);

Please read http://msdn.microsoft.com/en-us/library/fzhhdwae.aspx for marshalling of various types.

In C#

    [DllImport( "TestStrLib.dll" )]
    public static extern int TestFunction([In, Out] string[] stringArray
    , int size);

In C++

__declspec(dllexport) int TestFunction( char* ppStrArray[], int size)
   {
       return 0;
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文