大家帮我看看,这段代码有没有问题,为什么老是出现非法访问地址的错误 (100分)

  • 主题发起人 主题发起人 wjames
  • 开始时间 开始时间
W

wjames

Unregistered / Unconfirmed
GUEST, unregistred user!
这是delphi写的
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
Timer1: TTimer;
Label1: TLabel;
rgp1: TRadioGroup;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure rgp1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

type
Resolution =(SIF,QSIF,FULL);
_Resolution = set of Resolution;
{var
Resolution : _Resolution;}
type
ComPressMethod=(MPEG1,JPEG,BMP);
_ComPressMethod=set of ComPressMethod;
{var
ComPressMethod:= ComPressMethod; }

Standard=(NTSC,SQUARE,PAL);
_Standard=set of Standard;


StreamType=(VIDEOONLY,AUDIOONLY,BOTHSTREAM);
_StreamType=set of StreamType;


VideoAmp=(BRIGHTNESS,CONTRAST,HUE,SATURATION);
_VideoAmp=set of VideoAmp;

VIDEOPARAM=record
brightness:Integer;
contrast:Integer;
saturation:Integer;
hue:Integer;
end;

SNAPPARAM = record
compress : ComPressMethod;
Resolution : Resolution;
end;

pTSNP= ^ SNAPPARAM;

function MPG_Init(hMainWnd:HWND):boolean;stdcall;external 'PVMPEGI.DLL' name '_MPG_Init@4' ;
function MPG_OpenDisplay(hShowWnd:HWND;rcDisplay:TRECT;dwSlotNumber:DWORD):boolean;stdcall;external 'PVMPEGI.dll' name '_MPG_OpenDisplay@24';
function MPG_Exit():Boolean;stdcall;external 'PVMPEGI.DLL' name '_MPG_Exit@0';
function MPG_SetVideoStandard(std:Standard;dwSlotNumber:DWORD):Boolean;stdcall;external 'PVMPEGI.dll' name '_MPG_SetVideoStandard@8';
function MPG_Snap( SnapName:pChar; // File name
pSnapParam:SNAPPARAM; // Capture type.
dwSlotNumber:DWORD):Boolean;stdcall;external 'PVMPEGI.DLL' name '_MPG_Snap@12';
function MPG_IsVideoSourceOK(dwSlotNumber:DWORD):Boolean;stdcall;external 'PVMPEGI.dll' name '_MPG_IsVideoSourceOK@4';
implementation

{$R *.dfm}



[:(!]

[red]//---------------->>这里就出错了
procedure TForm1.Button3Click(Sender: TObject);
var snp:SNAPPARAM;
p_SNP:pTSNP;
begin
new(p_SNP);
p_SNP.compress:=BMP;
p_SNP.Resolution:=SIF;
MPG_Snap('image01.bmp',p_snp^,0);
end;[/red]
end.

这是原来的vc中的例子

//lib文件
typedef enum _ComPressMethod
{
MPEG1,
JPEG,
BMP,
} ComPressMethod;

typedef enum _Standard
{
NTSC,
SQUARE,
PAL
} Standard;

typedef enum _Resolution
{
SIF,
QSIF,
FULL
} Resolution;

typedef enum _VideoAmp
{
BRIGHTNESS,
CONTRAST,
HUE,
SATURATION
} VideoAmp;

typedef struct _SNAPPARAM
{
ComPressMethod compress;
Resolution resolution;
} SNAPPARAM;

// Initialize all DLL.
PVMPEGI_API BOOL WINAPI MPG_Init(HWND hMainWnd);
// DLL exit.
PVMPEGI_API BOOL WINAPI MPG_Exit();

// Open a display window for MPEG live video pass through.
PVMPEGI_API BOOL WINAPI MPG_OpenDisplay (HWND hShowWnd,
RECT rcDisplay, //Display area.
DWORD dwSlotNumber = 0);

// Snap capture, snap file will save to work path.
// SnapName : snap file name, don't include path.
PVMPEGI_API BOOL WINAPI MPG_Snap(LPSTR SnapName, // File name
SNAPPARAM *pSnapParam = NULL, // Capture type.
DWORD dwSlotNumber = 0); //Device ID (Card ID)


[:(][red]//-------------->>这是事件的代码
void CSampleDlg::OnBtnSnap()
{
SNAPPARAM snp;

snp.compress = JPEG;
snp.resolution = (Resolution)m_iResSize;

MPG_Snap( "Image0.JPG", // File name
&snp, // Capture type.
0); //Device ID (Card ID)
}[/red]
 
function MPG_Snap( SnapName:pChar; // File name
pSnapParam:SNAPPARAM;应该是指针类型吧?.
dwSlotNumber:DWORD):Boolean;stdcall;external 'PVMPEGI.DLL'
所以调用也应该是:
procedure TForm1.Button3Click(Sender: TObject);
var snp:SNAPPARAM;
p_SNP:pTSNP;
begin
new(p_SNP);
p_SNP.compress:=BMP;
p_SNP.Resolution:=SIF;
MPG_Snap('image01.bmp',p_snp,0);
end;

 
这样得到的结果不对的,在vc中&snp好像是取地址的
 
你试验过没有??你的PTSNP =^TSNP对不?
如果是,应该就是这样啦,VC里面这个东西是个结构,&nsp就是指针三
 
但是这样得不到正确的结果啊
 
我怀疑你的类型定义不正确。
尤其是
typedef struct _SNAPPARAM
{
ComPressMethod compress;
Resolution resolution;
} SNAPPARAM;
是否该转成
SNAPPARAM = record
compress : ComPressMethod;
Resolution : Resolution;
end;
delphi里这里的compress和resolution是字节对齐的,即sizeof(SNAPPARAM)=2。 是否vc里这两个枚举变量是word或dword对齐的,即sizeof(SNAPPARAM)=4或8?
 
SNAPPARAM = packed record
compress : ComPressMethod;
Resolution : Resolution;
end;
 
好像还是不正确
 
p_SNP:=new(SNAPPARAM);
...
dispose(p_SNP);
 
new(p_SNP);
p_SNP^.compress:=BMP;
p_SNP^.Resolution:=SIF;
MPG_Snap('image01.bmp',p_snp^,0);
dispose(p_SNP);
end;
 
这样也不对阿,连文件也没有创建阿
 
关注一下
 
我也怀疑是指针的问题。

比较赞成G5Studio的看法。
 
1.new 了以后要 dispose
2.怀疑是DLL参数调用的问题。检查PVMPEGI_API 是否为stdcall调用方式。试改为pascal看看。
 
function MPG_OpenDisplay(hShowWnd:HWND;rcDisplay:TRECT这里可能也错了,要指针;dwSlotNumber:DWORD):boolean;stdcall;external 'PVMPEGI.dll' name '_MPG_OpenDisplay@24';
function MPG_Snap( SnapName:pChar; // File name
pSnapParam:SNAPPARAM;这里也是 // Capture type.
dwSlotNumber:DWORD):Boolean;stdcall;external 'PVMPEGI.DLL' name '_MPG_Snap@12';
不相信你把头文件贴出来
 
这就是头文件中的定义
PVMPEGI_API BOOL WINAPI MPG_Snap(LPSTR SnapName, // File name
SNAPPARAM *pSnapParam = NULL, // Capture type.
DWORD dwSlotNumber = 0); //Device ID (Card ID)
PVMPEGI_API BOOL WINAPI MPG_OpenDisplay (HWND hShowWnd,
RECT rcDisplay, //Display area.
DWORD dwSlotNumber = 0);
 
你实验过C的sample能正常工作吗?
 
多人接受答案了。
 
后退
顶部