質問者
[VB2005]Delphiで作成したDLLの使用で「AccessViolationException」エラー

質問
-
初めて質問させて頂きます。よろしくお願い致します。
現在、VB2005でTextBoxに入力したパラメータを、Delphi2005で作成したDLL(AS/400へ接続するコンポーネントを含む)を通じてAS/400に渡し、AS/400上のPGMを実行し、戻り値をVBに返すという流れのプログラムを作成しております。【VB2005】
Option Explicit On
Imports System.Runtime.InteropServices
Public Class Form1
Declare Function vbParam Lib "VbToDel.dll" (ByVal p As String) As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As String
x = TextBox1.Text
TextBox2.Text = vbParam(x) ← エラー発生
End Sub
End Class
【DLL】
function vbParam(p : string) : string; stdcall;
begin//VBからパラメータ受取
Unit1.Form1.Call400.Value[0] := p;//AS400上のプログラム実行
Call400.Execute;//戻り値をVBに
Result := Unit1.Form1.Call400.Value[0];end;
exports
vbParam;VBのエラー発生の部分で、
「AccessViolationException はハンドルされませんでした。 保護されているメモリに読み取りまたは書き込み操作を行おうとしました。他のメモリが壊れていることが考えられます。」
というエラーが表示されます。
なぜエラーが起きてしまうのかご存知の方はご教授願えないでしょうか?
Delphiを使用していることもあり、質問をする場所を間違っていたら申し訳ありません。
すべての返信
-
http://msdn2.microsoft.com/ja-jp/library/system.accessviolationexception(VS.80).aspx
AccessViolationExceptionは、アンマネージコード(超簡単にいうと.NET以外のモジュール)で発生します。
問題はご利用のDelphiのモジュール側にあると思いますよ。
-
こんにちは
String型の引数は、Delphi側へ渡っていますか?
Delphiの方にデバッグ入れて、確認してみた方が良いと思います。
もし渡ってないなら、DelphiはPChar型を使って、VBはStringBuilderを使うとかですかね。
C++ですが、こちらが参考になるかな。
http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=3211&forum=7
-
試してみました。
Delphi側procedure DelphiStringTest(
AStr: PChar;
AStrResult: PChar; AResultBuffSize: Integer); stdcall;
begin
Windows.MessageBox(0, AStr, 'TEST', MB_OK);
StrPLCopy(AStrResult, string(AStr) + string(AStr), AResultBuffSize)
end;
exports DelphiStringTest;VB.NET側Imports System.Text
Public Class Form1
Private Declare Sub DelphiStringTest Lib "Project1.dll" ( _
ByVal pStr As StringBuilder, _
ByVal pStrResult As StringBuilder, ByVal pResultBuffSize As Integer)
Private Sub Button1_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim paramStr As New StringBuilder("てすと")
Dim resultStr As New StringBuilder(256)
DelphiStringTest(paramStr, resultStr, resultStr.Capacity)
MessageBox.Show(resultStr.ToString(), "TEST")
End Sub
End Class