Mac Office 2011 VBA和Dylib [英] Mac Office 2011 VBA and Dylib

查看:94
本文介绍了Mac Office 2011 VBA和Dylib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Mac OS中使用Word 2011插件.当前,我需要在VBA宏中编写代码以从另一个应用程序中检索字符串(通过Socket通信).因此,基本上在Windows中,我可以简单地制作一个DLL,该DLL可以帮助我与其他应用程序进行Socket通信,并将String值返回到VBA宏.

I'm working on a Word 2011 plugin in Mac OS. Currently, I need to write a code in VBA Macro to retrieve a String from another application (through Socket communication). So, basically in Windows, I can simply make a DLL which help me to do Socket communication with the other application and return the String value to VBA Macro.

但是,在Mac中,我可以构建.dylib(在C语言中)并使用VBA与dylib进行通信.但是,我在返回String时遇到了麻烦.我简单的C代码是这样的: char * tcpconnect(char *参数) {}

However, in Mac, I'm able to build a .dylib (in C) and using VBA to communicate with the dylib. However, I'm having a trouble with the return String. My simple C code is something like: char * tcpconnect(char* arguments) {}

首先,它始终包含Chr(0)字符.其次,我怀疑此C函数将无法处理Unicode字符串.

First, it always contains Chr(0) characters. Secondly, I suspected that this C function will not be able to handle Unicode String.

你们有经验或有类似的例子吗?

Do you guys have any experiences or have any similar example of this?

谢谢

大卫

推荐答案

我的原始帖子是尝试使用malloc()模仿SysAllocStringByteLen()的,但是当Excel尝试释放返回的内存时,这将失败.使用Excel分配该问题的内存修复程序,并且代码也更少,例如:

My original post was an attempt to imitate SysAllocStringByteLen() using malloc(), but this will fail when Excel tries to free the returned memory. Using Excel to allocate the memory fixes that issue, and is less code as well, e.g.:

在test.c中:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define LPCSTR const char *
#define LPSTR char *
#define __declspec(dllexport)
#define WINAPI

char *saved_string = NULL;
int32_t saved_len = -1;

#define _CLEANUP if(saved_string) free(saved_string)

__attribute__((destructor))
static void finalizer(void) {
  _CLEANUP;
}

int32_t __declspec(dllexport) WINAPI get_saved_string(LPSTR pszString, int cSize) {
  int32_t old_saved_len = saved_len;
  if(saved_len > 0 && cSize >= saved_len)
    strncpy(pszString, saved_string, saved_len);
  if(saved_string) {
    free(saved_string);
    saved_string = NULL;
    saved_len = -1;
  }
  return old_saved_len;
}

int32_t __declspec(dllexport) WINAPI myfunc(LPCSTR *pszString) {
  int len = (pszString && *pszString ? strlen(*pszString) : 0);
  saved_string = malloc(len + 5);
  saved_len = len + 5;
  sprintf(saved_string, "%s%.*s", "abc:", len, *pszString);
  return saved_len;
}

使用以上内容编译

gcc -g -arch i386 -shared -o test.dylib test.c

然后,在新的VBA模块中,使用以下命令并运行"test",它将在字符串"hi there"之前添加"abc:",并将结果输出到调试窗口:

Then, in a new VBA module, use the below and run "test", which will prepend "abc:" to the string "hi there" and output the result the debug window:

Public Declare Function myfunc Lib "<colon-separated-path>:test.dylib" (s As String) As Long
Public Declare Function get_saved_string Lib "<colon-separated-path>:test.dylib" (ByVal s As String, ByVal csize As Long) As Long

Option Explicit

Public Function getDLLString(string_size As Long) As String
    Dim s As String
    If string_size > 0 Then
        s = Space$(string_size + 1)
        get_saved_string s, string_size + 1
    End If
    getDLLString = s
End Function

Public Sub test()
Debug.Print getDLLString(myfunc("hi there"))
End Sub

这篇关于Mac Office 2011 VBA和Dylib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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