私たちの技術者の 1 人が、ファイル名と整数を渡す小さなライブラリ ファイルを作成するように私に依頼しました。ライブラリはテキスト ファイルをロードし、ファイル名によって参照されるテキスト ファイル内の整数を検索し、文字列をプログラムに返します。彼は、スクリプト言語として VB を使用するサード パーティ アプリケーションで作業しています。
そこで、彼の機器がインストールされている一部の古いマシンへの .net のインストールについて心配したくないので、C++ (VS 2010) に挑戦することにしました。私は C# でアプリケーション開発を行っており、最後に C++ コードをコンパイルしたのは VS 6 でした。さて、私はここに入力しているので、物事は明らかに間違った方向に進んでいます。私は C++ 側から始めました。
#include <WTypes.h>
#include <comutil.h>
#include <string.h>
BSTR _stdcall regExConv(BSTR fileName, int modNumber);
BSTR _stdcall regExConv(BSTR fileName, int modNumber) {
string inText;
// open the file and read in the text from the file
ifstream testFile;
testFile.open("C:\\Test\\testFile.txt", ifstream::in);
if(testFile.fail())
MessageBox(NULL,L"Failed to Open", NULL, NULL);
while (testFile.good())
getline(testFile, inText);
testFile.close();
_bstr_t passString(inText.c_str());
BSTR finalPass = passString.copy();
return finalPass;
}
それはうまくいきます。finalPass は、テキスト ファイルにあるテスト文字列を提供します。
Locals Window:
inText "eeeeeeeee" std::basic_string<char,std::char_traits<char>,std::allocator<char> >
passString {"eeeeeeeee" (1)} _bstr_t
finalPass 0x00722214 "eeeeeeeee" wchar_t *
VB側にはこれがあります。
Public Class Form1
Private Declare Function testFunc Lib "c:\Development\gTP3200\Debug\gTP3200.dll" (ByVal testVar As Integer) As Integer
Private Declare Function stringTest Lib "c:\Development\gTP3200\Debug\gTP3200.dll" (ByVal Str As String) As String
Private Declare Function regExConv Lib "c:\Development\gTP3200\Debug\gTP3200.dll" (ByVal fileString As String, ByVal faultedMod As Integer) As String
Private Declare Function stringBack Lib "c:\Development\gTP3200\Debug\gTP3200.dll" () As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim retVal As String
retVal = regExConv("file name pass", 3)
End Sub
End Class
問題は、VB 側に返される文字列が の最初の文字に過ぎないことinText
です。
Locals Window:
retVal "e" String
2日間、私はそれについてできる限りすべてを読み込もうとしましたが、モンティパイソンの悪いスキットにいるように感じ始めています. 何も機能していないようです。文字列を他のさまざまな型に変換してから BSTR に戻そうとしました。SysAlloc を試してみましたが、うまくいきませんでした。
String をパラメータとして BSTR に渡し、それを (ファイル名のように) 再度渡すと、VB 側で文字列全体が読み込まれます。