原文地址 https://www.peganza.com/delphi-and-outputdebugstring.html

Ever wanted to monitor your Delphi application in realtime, and be able to view log messages? Of course you can always run in full debug mode inside RAD Studio IDE. Another way is to output log messages, for example to a text file. You can also use the OutputDebugString function.

An advantage of this approach is that it impacts your application as little as possible. You don't have to care about file handling. Just call a function.

The OutputDebugString function is defined in the Win32 API, in the RTL unit Winapi.Windows.

You simply call it from your code like this:

1
2
3
4
5
6
7
8
9
10
11
..
uses
  Winapi.Windows;
..
var 
  Msg : string;
begin 
  OutputDebugString('This is my message');
  Msg := 'This is another log message';
  OutputDebugString(PChar(Msg));
..

If you call OutputDebugString repeatedly or in a loop, it may cause some performance overhead. So make sure that calls to OutputDebugString are not included in your release version. To avoid it, you can use a conditional compilation directive, like:

1
2
3
4
5
..
  (*$IFDEF ODS*)
  OutputDebugString('This is my message');
  (*$ENDIF*)
..

Only activate the compilation directive "ODS" when you want to log messages this way. Also, consider creating a wrapper function for the call to OutputDebugString, so your code will be easier to read and maintain. Something like this:

1
2
3
4
5
6
procedure DebugODS(const Msg : string);
begin
  (*$IFDEF ODS*)
  OutputDebugString(PChar(Msg));
  (*$ENDIF*)
end;

If you run from within RAD Studio IDE, the log messages will be displayed in the output window.

But if you run standalone, a good possibility is to use the free DebugView application (dbgview.exe) available from SysInternals (Microsoft). Information from their web page:

DebugView is an application that lets you monitor debug output on your local system, or any computer on the network that you can reach via TCP/IP. It is capable of displaying both kernel-mode and Win32 debug output, so you don't need a debugger to catch the debug output your applications or device drivers generate, nor do you need to modify your applications or drivers to use non-standard debug output APIs.

The image above shows some sample debug output from our Pascal Analyzer application.

As you see in the screenshot, messages are prefixed with a timestamp and a process ID. Read more about DebugView and download it here.

转载于:https://www.cnblogs.com/xalion/p/9792021.html

delphi 中OutputDebugString 函数的妙用(转载)相关推荐

  1. Delphi中 StrToIntDef函数的用法

    Delphi中 StrToIntDef函数的用法: 比如我要判断一个文本框里输入的字符串能不能转换为integer类型,如果能,则返回转换后的整型数据,如果不能,则返回整数0,那么我就可以用strto ...

  2. delphi中move函数的用法

    delphi中move函数的用法 -------------------------------------------------------------------------------- 20 ...

  3. 在Delphi中FormatDateTime函数的用法

    在Delphi中FormatDateTime函数的用法  function FormatDateTime(const Format: string; DateTime: TDateTime): str ...

  4. Delphi中类型转换函数

    类型转换函数 函数 功能 Chr 将一个有序数据转换为一个ANSI字符 Ord 将一个有序类型值转换为它的序号 Round 转换一个实型值为四舍五入后的整型值 Trunc 转换一个实型值为小数截断后的 ...

  5. 浅谈windows 编程中SendMessage函数的妙用!!!

    windows编程中SendMessage函数是非常重要的,而且这个对于理解windows的消息机制也很重要.本文用代码的方式实现了一些功能,借以说明此函数之妙用.说明不当之处,希望批评指正,谢谢! ...

  6. delphi中pos函数怎么用?

    pos(a,b)函数用法如下:取出子串a,在父串b中第一次出现的位置. 例如: pos('b','abcd'): 返回结果是2: 在delphi中使用汇编异常的简单,只用使用关键字asm ....en ...

  7. delphi中的函数传参如何传枚举参数_shell脚本的函数介绍使用和工作常用案例。建议收藏...

    #前言:今天我们来聊聊shell脚本中的函数知识,看一下函数的优势,执行过程和相关的使用案例. #简介 1.函数也具有别名类似的功能 2.函数是把程序里多次调用相同的代码部分定义成一份,然后给这份代码 ...

  8. Delphi中文件名函数-路径、名称、子目录、驱动器、扩展名

    文件名函数 文件名函数可以对文件的名称.所在子目录.驱动器和扩展名等进行操作.下表列出这些函数及其功能.函数说明ExpandFileName() //返回文件的全路径(含驱动器.路径) Extract ...

  9. delphi中move函数的用法 转

    我们能看到以下代码 var pSource,pDest:PChar;      len: integer; .......................//一些代码 Move(pSource,pDe ...

最新文章

  1. 泛型java博客园,Java深度历险之Java泛型
  2. solr与mysql数据同步的方案
  3. 上周热点回顾(12.8-12.14)
  4. GDCM:gdcm代码中引发bad_alloc异常测试程序
  5. [转]java垃圾回收之循环引用
  6. 《剑指offer》链表分割
  7. php util.js,javascript中一些util方法汇总_javascript技巧
  8. 智能计算机的功能是什么问题,人工智能在电脑系统的作用
  9. iOS 5与iOS 6的 low-memory 处理
  10. 从生活中领悟设计模式(Python)
  11. dw可以编辑java吗_用dreamweaver开发ASP图文教程。(修改资料篇)
  12. win7所有服务被禁用(应该是大多数被禁用)
  13. keep T 不是 KG等级_Lifestyle | 做Keep没有动力?一周的健身计划都在这里了!
  14. 张小龙的30条产品法则
  15. 卷土重来的FCoin日本站,你被割了吗?
  16. 大数据先导实践实验一
  17. 一个精壮的代购骗子被我用Python彻底征服了
  18. Basler|基于OpenCV的Basler相机采集图像程序
  19. PHP开发工程师(初级,中级,高级)
  20. 微信禁止访问国外服务器域名,域名被禁止访问?

热门文章

  1. OrthoVista 拼接线自动提取及匀色出图
  2. 测试服务器角色转移系统,角色转移功能全服开放
  3. 【天池】金融风控数据挖掘task1
  4. 什么待办软件可以设置闹钟来提醒办事
  5. 盘文件云存储——金山快盘
  6. 基于STM32+Openmv的追小球(颜色追踪)小车——一篇解决所有基本问题
  7. AMCL代码详解(三)创建粒子模型
  8. 刘知远:NLP研究入门之道(一)
  9. BGP BFD测试案例
  10. 北师大计算机在线作业答案0651,北师大版五年级数学上册全册教学同步练习随堂测试一课一练电子作业3份.docx...